Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » get id to be used in a foreign key with two model forms [RSS Feed]

#1 Dec. 23, 2010 03:49:13

Michael T.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

get id to be used in a foreign key with two model forms


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 athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 Dec. 23, 2010 11:49:27

Michael T.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

get id to be used in a foreign key with two model forms


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 athttp://groups.google.com/group/django-users?hl=en.

Offline

  • Root
  • » Django
  • » get id to be used in a foreign key with two model forms [RSS Feed]

Board footer

Moderator control

Enjoy the 23rd of October
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support