For those of you that are having the same issue with saving related
data in a model form here is how I solved it.
The issue is that I needed to save my form and then point to the saved
record in the related record before committing the related record.
I'm surprised django doesn't handle this automatically and know that
the related field is not a field, but a relation. It's defined as such
in the model.
The only case I could understand is if I tried to save the related
form before the base. Maybe I am doing it wrong? Anyone have any
thoughts?
if shirtForm.is_valid() and userForm.is_valid():
            temp=shirtForm.save()
            temp2=userForm.save(commit=False)
            temp2.shirt=temp
            shirtForm.save()
            temp2.save()
            return HttpResponseRedirect('/shirt/') # Redirect after
POST

On Dec 22, 10:49 pm, Michael Thamm <webmas...@morningcat.com> wrote:
> Hi,
> I am using 2 model forms and when I save the I get a save error since
> the foreign key field can't be blank.
> I try to add the new id at save, but it doesn't work.
> This is the code for the save.
>
> temp=shirtForm.save(commit=False)
>             userForm.shirt=temp.id
>             userForm.save()
>
> Is the newly created record id store in the field temp.id and can I
> assign that value directly as I am doing?
>
> Thanks
> Michael

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to