Django: How to increment a counter stored in models.py / Database -


i have created small counter model class counter store amount of times small survey application has been completed.

i want increment surveywizardonecounter in models.py def done() method in views.py

can tell me how this?

previously using global variables counter have found out not work web app. trying learn how create , store counter in db. appreciated.

models.py

class counter(models.model):                     surveywizardonecounter = models.smallintegerfield()     totalmaxcounter = models.smallintegerfield()  

views.py

from survey.models import counter  def done(self, form_list, **kwargs):      counter.surveywizardonecounter += 1  # trying increment counter      logger.debug('\n\n surveywizardonecounter = %s', surveywizardonecounter)      form in form_list:         form.save()      return render(self.request, 'return_to_amt.html', {         'form_data': [form.cleaned_data form in form_list],                 })   

since have 9 different survey counters , need increment each counter when survey submitted, better if define field survey_wizard_type possible values being survey_wizard_one, survey_wizard_two till survey_wizard_nine , field survey_wizard_count having default value 0 stores count particular survey wizard. there 9 records in database counter model then.

models.py

survey_wizard_type_choices = ['survey_wizard_one', 'survey_wizard_two', 'survey_wizard_three', 'survey_wizard_four', 'survey_wizard_five', 'survey_wizard_six', 'survey_wizard_seven', 'survey_wizard_eight', 'survey_wizard_nine']  class counter(models.model):       survey_wizard_type = models.charfield(choices=survey_wizard_type_choices)     survey_wizard_count = models.smallintegerfield(default=0)     total_max_counter = models.smallintegerfield()  

then in views.py, can use get_or_create method looking counter object given survey_wizard_type, creating 1 if necessary. increment survey_wizard_count 1 , save object database.

views.py

from django.db.models import f survey.models import counter  def done(self, form_list, **kwargs):      survey_counter = counter.objects.get_or_create(survey_wizard_type= 'survey_wizard_x') # x can value 1 9     survey_counter.survey_wizard_count = f('survey_wizard_count') + 1      survey_counter.save()      logger.debug('\n\n surveywizardonecounter = %s', surveywizardonecounter)      form in form_list:         form.save()      return render(self.request, 'return_to_amt.html', {         'form_data': [form.cleaned_data form in form_list],                 })    

edit

rahul provided solution code ended using

models.py

class counter(models.model):      survey_wizard_type_choices = (                               ('survey_wizard_one', 'survey_wizard_one'),                               ('survey_wizard_two', 'survey_wizard_two'),                                ('survey_wizard_three', 'survey_wizard_three'),                                ('survey_wizard_four', 'survey_wizard_four'),                                ('survey_wizard_five', 'survey_wizard_five'),                               ('survey_wizard_six', 'survey_wizard_six'),                                ('survey_wizard_seven', 'survey_wizard_seven'),                               ('survey_wizard_eight', 'survey_wizard_eight'),                               ('survey_wizard_nine', 'survey_wizard_nine'),                               )         survey_wizard_type = models.charfield(max_length=1000, choices=survey_wizard_type_choices)     survey_wizard_count = models.smallintegerfield(default=0)     total_max_counter = models.smallintegerfield(default=0)  

views.py

def done(self, form_list, **kwargs):      survey_counter = counter.objects.get_or_create(survey_wizard_type= 'survey_wizard_one')[0] # x can value 1 9     survey_counter.survey_wizard_count = f('survey_wizard_count') + 1      survey_counter.save()      form in form_list:         form.save()      return render(self.request, 'return_to_amt.html', {         'form_data': [form.cleaned_data form in form_list],                 })  

Comments