From 7bbb6958dcb6a450f926e5bb49b07391f801aef1 Mon Sep 17 00:00:00 2001 From: Loic Bistuer Date: Fri, 17 Jan 2014 01:24:39 +0700 Subject: 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. --- docs/ref/models/querysets.txt | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'docs') diff --git a/docs/ref/models/querysets.txt b/docs/ref/models/querysets.txt index a160587953..22ccda4b8e 100644 --- a/docs/ref/models/querysets.txt +++ b/docs/ref/models/querysets.txt @@ -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 `: + + >>> queryset = Pizza.objects.only('name') + >>> + >>> restaurants = Restaurant.objects.prefetch_related( + ... Prefetch('best_pizza', queryset=queryset)) + .. note:: The ordering of lookups matters. -- cgit v1.3