model attribute in a modelformset:
AttributeError at /number/
'WSGIRequest' object has no attribute 'qsession'
which occurs in the following snippet:
numbers = formset.save(commit=False)
for number in numbers:
number.qsession = request.qsession
number.save()
If I dir(number) I can see qsession as an attribute, which is
puzzling. I'm really bad at this stuff, so I'll put all the relevant
code below. Any help appreciated.
Here are the models:
class QuestionnaireSession(models.Model):
user = models.ForeignKey(User,blank=True,null=True)
name = models.CharField(max_length=100,blank=True,null=True)
class Number(models.Model):
qsession = models.ForeignKey(QuestionnaireSession)
name = models.CharField(max_length=100)
supertype = models.ForeignKey('self',blank=True,null=True)
And the formset:
NumberFormSet = modelformset_factory(Number, exclude=('qsession',))
And the relevant views.py code:
def get_qsession(request):
if 'qsession' not in request.session:
qsession = QuestionnaireSession()
qsession.save()
request.session['qsession'] = qsession
else:
qsession = request.session['qsession']
return qsession
def number(request):
qsession = get_qsession(request)
queryset = Number.objects.filter(qsession=qsession)
if request.method == 'POST': # form was submitted
formset = NumberFormSet(request.POST, queryset=queryset)
if formset.is_valid():
numbers = formset.save(commit=False)
for number in numbers:
number.qsession = request.qsession
number.save()
return HttpResponseRedirect('/top/')
else:
formset = NumberFormSet(queryset=queryset)
return render_to_response('questionnaire/questionnaire.html',
{'numberformset': formset},
context_instance=RequestContext(request))
Thank you
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
No comments:
Post a Comment