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

2010-05-12 Thread Nick Serra
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-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.



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

2010-05-12 Thread Daniel Roseman
On May 12, 4:04 pm, Nick Serra  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 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.



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

2010-05-12 Thread Nick Serra
Thanks for the response. I actually will already have the PK, and am
trying to avoid grabbing an instance of the actual item object.

On May 12, 11:26 am, Daniel Roseman  wrote:
> On May 12, 4:04 pm, Nick Serra  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 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.

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



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

2010-05-12 Thread Daniel Roseman
On May 12, 4:34 pm, Nick Serra  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.)
--
DR.

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



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

2010-05-12 Thread Nick Serra
Exactly what I was looking for, thanks!

On May 12, 11:50 am, Daniel Roseman  wrote:
> On May 12, 4:34 pm, Nick Serra  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.)
> --
> DR.
>
> --
> 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.

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