On Sat, Aug 31, 2013 at 9:01 PM, Gerd Koetje
<deonlinefotograaf@gmail.com> wrote:
> Hi,
>
>
> Why does this save sometimes not save all forms
> If i set form8 at the top it doesnt save form 8
> When i set it at the bottom it does save form 8, but not any others
>
> without the form8 all works
>
>
> if request.POST:
>
> form = ProfielenForm(request.POST, instance=request.user.profile)
> form2 = ProfielenForm2(request.POST, instance=request.user.profile)
> form3 = ProfielenForm3(request.POST, instance=request.user.profile)
> form4 = ProfielenForm4(request.POST, instance=request.user.profile)
> form5 = ProfielenForm5(request.POST, instance=request.user.profile)
> form6 = ProfielenForm6(request.POST, instance=request.user.profile)
> form7 = ProfielenForm7(request.POST, instance=request.user.profile)
> form8 = ProfielenForm8(request.POST, instance=request.user.profile)
>
>
>
> if form7.is_valid():
> form7.save()
>
> if form6.is_valid():
> form6.save()
>
> if form5.is_valid():
> form5.save()
>
> if form4.is_valid():
> form4.save()
>
> if form3.is_valid():
> form3.save()
>
> if form2.is_valid():
> form2.save()
>
> if form.is_valid():
> form.save()
>
> if form8.is_valid():
> form8.save()
>
All the forms don't save because all the forms are not valid. A form
that is not valid will have errors associated with the invalid field,
or non field errors associated with the form - make sure you are
outputting them in the template so you can see them.
If you want to structure your form like this, as a series of different
form classes all inside a single <form> element, then you can do this,
but make sure that you do not save the form unless ALL form instances
are correct.
You must also make sure that you pass a single instance of the object
being modified to each form. If you pass a different instance to each
form, then each form will save just its changes to the instance, which
are then wiped out immediately by the next form. Eg, start with this:
instance = request.user.profile
if request.method == 'POST':
form = ProfielenForm(request.POST, instance=instance)
form2 = ProfielenForm2(request.POST, instance=instance)
form3 = ProfielenForm3(request.POST, instance=instance)
if form.is_valid() and form2.is_valid() and form3.is_valid():
form.save()
form2.save()
form3.save()
else:
form = ProfielenForm(instance=instance)
form2 = ProfielenForm2(instance=instance)
form3 = ProfielenForm3(instance=instance)
return render_to_response(...)
If the forms do not save, and no errors are shown, then you aren't
showing the errors in the template.
Another thing that could potentially go wrong, if the current user has
no profile instance, then request.user.profile will be None. Passing
None as the instance to all these model forms is not an error, but
will create a new profile instance for each form that you save, likely
none of them linked to the current user. I could see how that
behaviour could be described as "none of the forms save anything".
Cheers
Tom
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment