On Fri, Jul 17, 2009 at 11:06 PM, Miriam<magol...@gmail.com> wrote: > > There currently is no way in Django to pre-fetch one-to-many and/or > many-to-many relationships, right?
Correct. > There's select_related, which will pre-fetch FK or one-to-one, which > is great, but not sufficient. > > Suppose I am fetching a list of Blog posts and I want to pre-fetch the > Comments for each of these posts? There's no way to do that in > Django. > > I was looking through the tickets at code.djangoproject.com and I > didn't see anything about this. > > Does anyone know if this feature is planned for sometime down the > road? Anyone have a work-around or a patch or anything. This is > supremely irritating. You're not the first to suggest the idea. There are two (well, three) obstacles that are standing in the way of this idea: Firstly - What's the interface to this? Is it just an extension of the capabilities of select_related? If so - how does the proposed interface map to SQL? In particular, what happens to the results in A that are duplicated when you do a select_related() onto B? Second - How do we constrain the performance implications? For example, if a table with just 3 rows does a select_related() on a table that contains 10000 rows, how do you prevent an query accidentally bringing the system to it's knees? Third - someone needs to actually write the code. However, this is a non-issue until the first two are resolved. The reason select_related() has been implemented for the N-1 case, but not the 1-N case is that the answer to the first two questions is easy. Since the relationship has cardinality 1, all you're doing is extending the result line. There's no need to worry about handling of repeated results; performance problems are limited to concerns of the depth that select_related will traverse. If you have any particularly inspired answers to these questions for the 1-N case, feel free to let us know. We're always interested in making Django better. Yours, Russ Magee %-) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@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 -~----------~----~----~----~------~----~------~--~---