Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » Populate Foreign Key after Form is Submitted [RSS Feed]

#1 June 14, 2010 21:59:37

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

Populate Foreign Key after Form is Submitted


Hello all,

I have a model A that has >4000 records in it. I have just created a
new model B that has a foreign key to A. I want to be able to create
an instance of B and save it which is no problem going the normal
routes. However, I like to use Ajax to make the load times faster
because a lot of our clients are on slow connections in China. With
the foreign key <option> field being populated by 4000+ values, the
load time for the page is quite slow. I would like to leave
ModelChoiceField off my form and then populate it in my view (or a
hidden input in my form). However, whenever I just try to save the pk
number value into the foreign key, I get the error "Cannot assign
"u'1783'": "MainNode.customers" must be a "Customers" instance."

How can I just save a number to the foreign key field? Thanks for any
advice and help.

Greg

--
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 June 14, 2010 22:04:01

Lee H.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Populate Foreign Key after Form is Submitted


On Mon, Jun 14, 2010 at 1:59 PM, geraldcor <gregco...@gmail.com> wrote:

> Hello all,
>
> I have a model A that has >4000 records in it. I have just created a
> new model B that has a foreign key to A. I want to be able to create
> an instance of B and save it which is no problem going the normal
> routes. However, I like to use Ajax to make the load times faster
> because a lot of our clients are on slow connections in China. With
> the foreign key <option> field being populated by 4000+ values, the
> load time for the page is quite slow. I would like to leave
> ModelChoiceField off my form and then populate it in my view (or a
> hidden input in my form). However, whenever I just try to save the pk
> number value into the foreign key, I get the error "Cannot assign
> "u'1783'": "MainNode.customers" must be a "Customers" instance."
>
> How can I just save a number to the foreign key field? Thanks for any
> advice and help.
>
> Greg
>

If you're talking about this problem in Admin, look at:http://docs.djangoproject.com/en/1.2/ref/contrib/admin/#django.contrib.admin.ModelAdmin.raw_id_fieldsIf it's your own form, you could do something similar.


>

--
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

#3 June 14, 2010 22:13:40

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

Populate Foreign Key after Form is Submitted


That was perfect. Thank you. I don't look at the admin docs enough I
guess. The following worked:

customers = forms.ModelChoiceField(queryset=Customers.objects.all(),
widget=forms.TextInput)

On Jun 14, 3:03 pm, Lee Hinde <leehi...@gmail.com> wrote:
> On Mon, Jun 14, 2010 at 1:59 PM, geraldcor <gregco...@gmail.com> wrote:
> > Hello all,
>
> > I have a model A that has >4000 records in it. I have just created a
> > new model B that has a foreign key to A. I want to be able to create
> > an instance of B and save it which is no problem going the normal
> > routes. However, I like to use Ajax to make the load times faster
> > because a lot of our clients are on slow connections in China. With
> > the foreign key <option> field being populated by 4000+ values, the
> > load time for the page is quite slow. I would like to leave
> > ModelChoiceField off my form and then populate it in my view (or a
> > hidden input in my form). However, whenever I just try to save the pk
> > number value into the foreign key, I get the error "Cannot assign
> > "u'1783'": "MainNode.customers" must be a "Customers" instance."
>
> > How can I just save a number to the foreign key field? Thanks for any
> > advice and help.
>
> > Greg
>
> If you're talking about this problem in Admin, look at:
>
>http://docs.djangoproject.com/en/1.2/ref/contrib/admin/#django.contri...
>
> If it's your own form, you could do something similar.
>
>
>
>

--
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
  • » Populate Foreign Key after Form is Submitted [RSS Feed]

Board footer

Moderator control

Enjoy the 16th of December
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