Saving objects that have foreign key fields using the pk instead of an instance of the foreignkeyed object

3,352 views
Skip to first unread message

Nick Serra

unread,
May 12, 2010, 11:04:41 AM5/12/10
to Django users
Sorry for the confusing title. Here is a simple example to illustrate
my question:

class Item(models.Model):
name = models.CharField()

class Manager(models.Model):
item = models.ForeignKey(Item)


On a POST I have the PK of the Item I want to link to a new instance
of the Manager object. I am saving a new Manager object that will be
linked to the Item with the PK i have. So I can do:

item_pk = request.POST.get('item')
item = Item.objects.get(pk=item_pk)
new_manager = Manager(item=item)
new_manager.save()

...but I don't like the idea of grabbing the item object first. Is
there any way to just save the new Manager object with just the PK of
the Item, thus avoiding the database call to grab the Item object?

Thanks!

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

Daniel Roseman

unread,
May 12, 2010, 11:26:30 AM5/12/10
to Django users
On May 12, 4:04 pm, Nick Serra <nickse...@gmail.com> wrote:
> Sorry for the confusing title. Here is a simple example to illustrate
> my question:
>
> class Item(models.Model):
>     name = models.CharField()
>
> class Manager(models.Model):
>     item = models.ForeignKey(Item)
>
> On a POST I have the PK of the Item I want to link to a new instance
> of the Manager object. I am saving a new Manager object that will be
> linked to the Item with the PK i have. So I can do:
>
> item_pk = request.POST.get('item')
> item = Item.objects.get(pk=item_pk)
> new_manager = Manager(item=item)
> new_manager.save()
>
> ...but I don't like the idea of grabbing the item object first. Is
> there any way to just save the new Manager object with just the PK of
> the Item, thus avoiding the database call to grab the Item object?
>
> Thanks!
>

You should just be able to do
manager = Manager(item=item_pk)
but in general, you can always refer to item_id, as the underlying
database field representing the foreign key value.
--
DR.

Nick Serra

unread,
May 12, 2010, 11:34:42 AM5/12/10
to Django users
Thanks for the response. I actually will already have the PK, and am
trying to avoid grabbing an instance of the actual item object.

Daniel Roseman

unread,
May 12, 2010, 11:50:35 AM5/12/10
to Django users
On May 12, 4:34 pm, Nick Serra <nickse...@gmail.com> wrote:
> Thanks for the response. I actually will already have the PK, and am
> trying to avoid grabbing an instance of the actual item object.

Yes, that's what I meant. You can assign the foreign key directly when
instantiating an object, without having to get the item object by
using the FOO_id field.

manager = Manager(item_id=mypkvalue)

(sorry, previous example was missing the "_id" suffix.)

Nick Serra

unread,
May 12, 2010, 11:58:50 AM5/12/10
to Django users
Exactly what I was looking for, thanks!
Reply all
Reply to author
Forward
0 new messages