On 26 marras, 21:36, tolomea <gordon.wrig...@gmail.com> 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 post to this group, send email to django-developers@googlegroups.com. To unsubscribe from this group, send email to django-developers+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-developers?hl=en.