Does the ORM support eager fetches?

194 views
Skip to first unread message

meppum

unread,
Apr 18, 2008, 6:48:01 PM4/18/08
to Django users
I was wondering if it was possible to load a parent object and it's
child objects at the same time instead of lazily loading the children
after the parent has been retrieved?

Peter Rowell

unread,
Apr 18, 2008, 7:24:33 PM4/18/08
to Django users
Take a look at select_related(), it may be of some help, depending on
how you define "parent" and "child".
http://www.djangoproject.com/documentation/db-api/#select-related

"Returns a QuerySet that will automatically “follow” foreign-key
relationships, selecting that additional related-object data when it
executes its query. This is a performance booster which results in
(sometimes much) larger queries but means later use of foreign-key
relationships won’t require database queries."

meppum

unread,
Apr 18, 2008, 8:57:30 PM4/18/08
to Django users
I read that to mean that it follows foreign key relationships "up"
from the child to the parent. Meaning that if you load
Parent.select_related() that parent.child_set() will not yet have been
loaded. Or did I read it wrong?

On Apr 18, 7:24 pm, Peter Rowell <hedron...@gmail.com> wrote:
> Take a look at select_related(), it may be of some help, depending on
> how you define "parent" and "child".http://www.djangoproject.com/documentation/db-api/#select-related

alex....@gmail.com

unread,
Apr 18, 2008, 11:32:09 PM4/18/08
to Django users
No, you are correct, it does not follow reverse foreign keys, at least
not yet, there was a discussion about this a few days ago, if this is
of importance to you, you could either held write the code for django
to support following reverse foreign keys in select_related() or you
could write the SQL manually.

Malcolm Tredinnick

unread,
Apr 18, 2008, 11:59:43 PM4/18/08
to django...@googlegroups.com

On Fri, 2008-04-18 at 17:57 -0700, meppum wrote:
> I read that to mean that it follows foreign key relationships "up"
> from the child to the parent. Meaning that if you load
> Parent.select_related() that parent.child_set() will not yet have been
> loaded. Or did I read it wrong?

See a post from earlier this week about why select_related() for
anything that returns multi-valued results (which includes reverse
ForeignKeys) is not implemented yet and won't be for a while:


http://groups.google.com/group/django-users/browse_frm/thread/53f30fd617f13f1d/18ecb827406d2b8f?lnk=gst&q=select_related#18ecb827406d2b8f
Regards,
Malcolm

--
A clear conscience is usually the sign of a bad memory.
http://www.pointy-stick.com/blog/

Reply all
Reply to author
Forward
0 new messages