You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
because the queryset (Pizza.objects.all()) is lazy. The database query doesn't actually get run until the queryset is iterated - which happens in the template! So, django-zen-queries provides a tiny helper function, fetch, which forces evaluation of a queryset:
I've always thought that when .all(), .first(), etc are added, then the fetching of data happens immediately.
Any methods that return either a new queryset or a copy of the current queryset .. they are all lazy loading. They do not fetch.
So I know this repeats a bit of the Django documentation but would be extremely helpful to reiterate this.
NO need to do as I suggest. This is just a suggestion. I will close this myself within 24 hrs because rightfully this should be in a Discussion, but I don't see Discussions here, hence I opened as an issue.
List of methods that return Queryset hence lazy-loading.
In my own case, I will say, it's because I have a "bug" in my mental model about Django ORM methods.
I thought all() doesn't return queryset, but actually it does.
I am right about first() though.
Perhaps the balance is to say,
## How to solve surprising errors query errors after including zen_queries
If you are surprised about errors that arise after you include zen_queries.render or zen_queries.TemplateResponse, perhaps you have a buggy mental model about certain ORM methods.
Note that Django docs (insert link to Django docs under dev branch) state the full list of ORM methods that return QuerySet, whereas these are the ORM methods that do not.
I am thikning on my feet here. I am not sure if this is ideal.
I've always thought that when
.all()
,.first()
, etc are added, then the fetching of data happens immediately.Now that this zen-queries force me to re-read this section https://docs.djangoproject.com/en/4.0/ref/models/querysets/#methods-that-return-new-querysets I now realize
Any methods that return either a new queryset or a copy of the current queryset .. they are all lazy loading. They do not fetch.
So I know this repeats a bit of the Django documentation but would be extremely helpful to reiterate this.
NO need to do as I suggest. This is just a suggestion. I will close this myself within 24 hrs because rightfully this should be in a Discussion, but I don't see Discussions here, hence I opened as an issue.
List of methods that return Queryset hence lazy-loading.
List of methods that do not return querysets and usually about retrieving data
The text was updated successfully, but these errors were encountered: