Does `.all()` in Django re-query the Database or use the cache?
I am getting very conflicting messages. From Django Queryset docs, I am reading (https://docs.djangoproject.com/en/5.1/ref/models/querysets/#django.db.models.query.QuerySet.all):
When a QuerySet is evaluated, it typically caches its results. If the data in the database might have changed since a QuerySet was evaluated, you can get updated results for the same query by calling all() on a previously evaluated QuerySet.
But then under the prefetch_related
section, it shows that using .all()
on the sub-objects uses results from the cache:
>>> restaurants = Restaurant.objects.prefetch_related(
... Prefetch("pizzas", queryset=queryset),
... )
>>> vegetarian_pizzas = restaurants[0].pizzas.all()
Here, apparently vegetarian_pizzas
doesn't trigger a database query.
So...which is it - what does all()
actually do? Why does it trigger a database query on the outer but then not for sub-objects? I'm confused.