Re: Access Foreignkey ID without doing query? (Merging Foreignkey/M2M) (manual select_related)

2010-07-12 Thread Jari Pennanen
Uhh, you are right... it does work!

I though I was writing a pseudo code (without testing that my pseudo
code indeed worked already)... Sometimes Django is intuitive to extent
it guesses what I wanted!

Though foreignkey documentation did not include information that I can
access the raw ID like "host_id".

And BTW I ended up using dict to cache the ID's myself too.

On 12 heinä, 15:21, Daniel Roseman  wrote:
> On Jul 12, 11:29 am, Jari Pennanen  wrote:
>
>
>
>
>
> > Hi!
>
> > Is it possible to access the foreignkey ID value of model? I do not
> > want Django ORM to try to make query because it fails in this case. I
> > have exact same problem 
> > ashttp://groups.google.com/group/django-users/browse_thread/thread/d24f...
> > (though I'm not using admin, I have list view of my own)
>
> > I will use the same example:
>
> > Host
> >     name = CharField
> > Account
> >     name = CharField
> >     host = ForeignKey(Host, related_name='accounts')
>
> > I have list of "Host" where I need to also list on each item a list of
> > accounts. Now if I could access the raw "Account.host" foreignkey ID I
> > could do this in two queries:
>
> > hosts = list(Host.objects.all())
> > accounts = list(Account.objects.filter(host__in=hosts))
>
> > for h in hosts:
> >   h.account_list = [a for a in accounts if a.host_id == h.id] #
> > NOTICE: THIS is not possible! I cannot access the foreignkey host_id :
> > (
>
> Why not? What happens? This is the correct way to access the foreign
> key ID.
>
> Note that if you get this working, you'll be iterating through all the
> selected accounts each time, for every Host. If you've got large
> numbers of accounts, there is a slightly more efficient way of doing
> this, by using dictionaries to pre-group the accounts by their
> matching hosts: see my blog[1] for an explanation.
> --
> DR.
>
> [1]:http://blog.roseman.org.uk/2010/01/11/django-patterns-part-2-efficien...

-- 
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: Access Foreignkey ID without doing query? (Merging Foreignkey/M2M) (manual select_related)

2010-07-12 Thread Daniel Roseman
On Jul 12, 11:29 am, Jari Pennanen  wrote:
> Hi!
>
> Is it possible to access the foreignkey ID value of model? I do not
> want Django ORM to try to make query because it fails in this case. I
> have exact same problem 
> ashttp://groups.google.com/group/django-users/browse_thread/thread/d24f...
> (though I'm not using admin, I have list view of my own)
>
> I will use the same example:
>
> Host
>     name = CharField
> Account
>     name = CharField
>     host = ForeignKey(Host, related_name='accounts')
>
> I have list of "Host" where I need to also list on each item a list of
> accounts. Now if I could access the raw "Account.host" foreignkey ID I
> could do this in two queries:
>
> hosts = list(Host.objects.all())
> accounts = list(Account.objects.filter(host__in=hosts))
>
> for h in hosts:
>   h.account_list = [a for a in accounts if a.host_id == h.id] #
> NOTICE: THIS is not possible! I cannot access the foreignkey host_id :
> (

Why not? What happens? This is the correct way to access the foreign
key ID.

Note that if you get this working, you'll be iterating through all the
selected accounts each time, for every Host. If you've got large
numbers of accounts, there is a slightly more efficient way of doing
this, by using dictionaries to pre-group the accounts by their
matching hosts: see my blog[1] for an explanation.
--
DR.

[1]: 
http://blog.roseman.org.uk/2010/01/11/django-patterns-part-2-efficient-reverse-lookups/

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



Access Foreignkey ID without doing query? (Merging Foreignkey/M2M) (manual select_related)

2010-07-12 Thread Jari Pennanen
Hi!

Is it possible to access the foreignkey ID value of model? I do not
want Django ORM to try to make query because it fails in this case. I
have exact same problem as 
http://groups.google.com/group/django-users/browse_thread/thread/d24f2a502da3171c?pli=1
(though I'm not using admin, I have list view of my own)

I will use the same example:

Host
name = CharField
Account
name = CharField
host = ForeignKey(Host, related_name='accounts')

I have list of "Host" where I need to also list on each item a list of
accounts. Now if I could access the raw "Account.host" foreignkey ID I
could do this in two queries:

hosts = list(Host.objects.all())
accounts = list(Account.objects.filter(host__in=hosts))

for h in hosts:
  h.account_list = [a for a in accounts if a.host_id == h.id] #
NOTICE: THIS is not possible! I cannot access the foreignkey host_id :
(

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