django - Exception Value: global name 'total_max_counter' is not defined -
i trying create new table in mysql db single field record incrementing value. keep getting error:
exception value: global name 'total_max_counter' not defined
can tell me going wrong?
views.py
from survey.models import totalcounter def done(self, form_list, **kwargs): total_counter = totalcounter.objects.get_or_create(total_max_counter) total_counter.survey_wizard_total += 1 total_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], })
models.py
class totalcounter(models.model): total_max_counter = models.smallintegerfield(default=0) def __unicode__(self): return self
firstly, in get_or_create()
, need specify argument in form of kwargs
. only, object given kwargs
, creating 1 if necessary. kwargs
may empty if model has defaults fields.
the function definition get_or_create()
is:
get_or_create(defaults=none, **kwargs)
secondly, trying update survey_wizard_total
field of totalcounter
object whereas model has no such field defined in it. has field total_max_counter
in it. need correct in code.
this should work you:
from django.db.models import f survey.models import totalcounter def done(self, form_list, **kwargs): total_counter = totalcounter.objects.get_or_create(total_max_counter__gte=0)[0] total_counter.total_max_counter = f('total_max_counter') + 1 # increment value of `total_max_counter` 1 total_counter.save() # save object form in form_list: form.save() return render(self.request, 'return_to_amt.html', { 'form_data': [form.cleaned_data form in form_list], })
Comments
Post a Comment