Allowed custom querysets when prefetching single valued relations

The original patch for custom prefetches didn't allow usage of custom
queryset for single valued relations (along ForeignKey or OneToOneKey).
Allowing these enables calling performance oriented queryset methods like
select_related or defer/only.

Thanks @akaariai and @timgraham for the reviews. Refs #17001.
This commit is contained in:
Loic Bistuer 2014-01-17 01:24:39 +07:00 committed by Anssi Kääriäinen
parent c8d61fa109
commit 7bbb6958dc
5 changed files with 112 additions and 23 deletions

View file

@ -989,6 +989,24 @@ less ambiguous than storing a filtered result in the related manager's cache:
... Prefetch('pizzas', queryset=queryset))
>>> vegetarian_pizzas = restaurants[0].pizzas.all()
Custom prefetching also works with single related relations like
forward ``ForeignKey`` or ``OneToOneField``. Generally you'll want to use
:meth:`select_related()` for these relations, but there are a number of cases
where prefetching with a custom ``QuerySet`` is useful:
* You want to use a ``QuerySet`` that performs further prefetching
on related models.
* You want to prefetch only a subset of the related objects.
* You want to use performance optimization techniques like
:meth:`deferred fields <defer()>`:
>>> queryset = Pizza.objects.only('name')
>>>
>>> restaurants = Restaurant.objects.prefetch_related(
... Prefetch('best_pizza', queryset=queryset))
.. note::
The ordering of lookups matters.