Thursday, September 29, 2011

Re: modelformset partial save and AttributeErrors

Ahem... turned out to be quite a small problem.

number.qsession = request.qsession

should be

number.qsession = request.session['qsession']

Sorry for the list noise. But if anybody has a better idea for how to
tie data from multiple forms together with a common "session" (not
necessarily a Django session), besides putting all the data in the
session dictionary, then I'd be happy to hear them. Thanks

On Sep 28, 10:46 pm, Michael Wayne Goodman <goodman....@gmail.com>
wrote:
> Hi, I'm getting the following error when I try to assign a value to a
> 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