On Mon, Oct 10, 2016 at 12:56 PM, Geoff Kuenning <geoff@cs.hmc.edu> wrote:
album = album.key)
So is there a way to get Django to issue this join and collect the
results into an aggregate set? I've read through the docs and
experimented with different filter and double-underscore notations,
without success. It seems silly to do the work with a loop when a
correctly written join can do it so much faster. (And I'll note that if
AlbumOrder had more entries, the loop would take far longer.)
Hi Geoff,
Have you looked at the docs for `select_related` and `prefetch_related`? It sounds like they maybe what you're looking for here:
So your example query:
> for photo in photos.filter(albumorder__album = album.key):
photo.ordering = photo.albumorder_set.filter(Would be something like `for photo in photos.filter(albumorder__album = album.key).select_related('albumorder'): ...`
You need to tell Django which related tables to include in the initial SQL query, and select_related and prefetch_related accomplish this, in subtly different ways (will let you read the docs to figure that out since I haven't quite myself ;-) )
Hope this helps! select_related has gotten me out of many DB optimization issues in the past and now I'll never ship code without carefully examining where all using a `select_related` would significantly speed up queries.
-Sanjay
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAG3W7ZEWsyZ4pdtURVXiRummSpOMkHHZSOte--3FrrM2VX7XxQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment