This is not an accurate statement. At no point within the docs for this function does it make any reference to recursion.
This method is used when you have a list of objects, and not a queryset.
Quoting directly from the docs you referenced:
This is useful in code that receives a list of model instances as opposed to a
QuerySet
; for example, when fetching models from a cache or instantiating them manually.
For example, using the classes in the docs, you might write:
restaurants = Restaurant.objects.all().prefetch_related('pizzas')
This use of the prefetch_related
is being applied to a queryset (Restaurant.objects.all()
)
Now, let’s say you have a list of Restaurant objects that is not a queryset:
restaurants = list(Restaurant.objects.all())
This list is no longer a queryset. You cannot do something like:
restaurants_with_pizzas = restaurants.prefetch_related('pizzas')
(If you try this, you will get an AttributeError: 'list' object has no attribute 'prefetch_related'
.)
However, what you can do is:
restaurants_with_pizzas = prefetch_related_objects(restaurants, 'pizzas')