On Apr 18, 5:32 pm, Daishy <dai...@web.de> wrote:
> Hi together,
>
> I'm trying to use select_related in a query, but it seems i'm doing
> something wrong. I have googled, but not really found anything (Maybee
> used the wrong keywords :/)
>
> I've got a model that is related to another model, roughly like that:
>
> class A(models.Model):
>   a = models.CharField()
>
> class B(models.Model):
>   link = models.ForeignKey(A)
>
> Now i want to query all entries in B along with the related models,
> which would be the following (?)
> objs = B.objects.select_related().all()
> item = objs[0]
>
> This produces one correct join-statement and populates all objects.
> Now my problem: I need the keys of B / link.pk. Unfortunately item.pk,
> item.id, item.link.pk, item.link.id gives me None as result. Do i need
> to specify some other options, or is that just the default behaviour?
> If so, is there a way to fill the ids anyhow? (I've got 30 rows with
> ~2 foreign-keys each and all of them get displayed, which hits the db
> quite often.)
> I'm using Django from the trunk.
>
> Thanks for any help!
> Daishy
>

I'm really not sure what you're asking here.

If you execute the code you've given and then print item.id or
item.pk, that will give you the primary key of the B object that you
got originally. If you print item.link.id, that will give you the
primary key of the A object that is related to the B object you have.
You don't need any extra code to do this.

(In fact, if you only need the primary key of the related A object,
you don't even need to do select_related - you can just do
item.link_id, as this is an attribute of the B model not the A one.
But presumably you're after more attributes than just the pk.)
--
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.

Reply via email to