On May 12, 4:04 pm, Nick Serra <[email protected]> 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.

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

Reply via email to