Yup, qs.prefetch_related('best_pizza__toppings__topping_type',
use_select_related=True) looks good enough for many cases.
We have a lot of cases in the code where this would save many queries.
On Tuesday, November 27, 2012 2:57:16 PM UTC, Anssi Kääriäinen wrote:
>
> On 26 marras, 21:36, tolomea <[email protected]> wrote:
> > The focus then is on how to let the developer specify what they want in
> the
> > same query and what they want in a different query.
> > Select_related currently adds same query stuff and prefetch_related
> > adds separate query stuff. So what if we added a way for a
> select_related
> > to be bolted onto a prefetch_related. A "base" argument to
> select_related
> > seems like a reasonable way of doing this. The original example was:
> >
> > >
> Restaurant.objects.prefetch_related('best_pizza__toppings__topping_type')
> >
> > We could rewrite it as:
> >
> >
> >
> >
> Restaurant.objects.prefetch_related('best_pizza__toppings").select_related(base='best_pizza__toppings",
>
>
> > "topping_type')
> >
> > The prefetch_related can be inferred from the base, so we could shorten
> it
> > to:
> >
> > > Restaurant.objects.select_related(base='best_pizza__toppings",
> >
> > "topping_type')
> >
> > Each unique unique base produce a new db query, multiple select_relateds
> > with the same base are done as one query.
> > I think that's actually a complete system in itself, prefetch_related is
> > just shorthand and can be implemented as:
> >
> > def prefetch_related(queryset, *args):
> > for arg in args:
> > queryset = queryset.select_related(base=arg)
> > return queryset
> >
> > The only quirk comes from the original example again, what does this do?
> >
> >
> Restaurant.objects.prefetch_related('best_pizza__toppings__topping_type')
> >
> > Tired, but I think there is something to this line of thinking.
>
> The above could work. I would maybe do it the other way, something
> along lines: qs.prefetch_related('best_pizza__toppings__topping_type',
> use_select_related=True). This would tell the prefetching to use
> automatically select_related for any lookups encountered assuming it
> is possible to select_related the data. For those lookups which are
> multijoins prefetch would work as always. If you want to prefetch the
> best_pizza, but select_related the topping_type then use
> qs.prefetch_related('best_pizza__toppings').prefetch_related('best_pizza__toppings__topping_type',
>
>
> use_select_related=True)
>
> Custom queryset use would also be useful. Maybe you want to order the
> toppings some other way than the default ordering. If a nice API
> surfaces for this this would be a very nice addition.
>
> - Anssi
>
--
You received this message because you are subscribed to the Google Groups
"Django developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-developers.
For more options, visit https://groups.google.com/groups/opt_out.