Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Please emphasize the lazy queryset in your section covering fetch #48

Open
simkimsia opened this issue Jul 19, 2022 · 3 comments
Open

Comments

@simkimsia
Copy link

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.

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.

image

List of methods that do not return querysets and usually about retrieving data

@j4mie
Copy link
Member

j4mie commented Jul 19, 2022

Thanks for raising this. What do you think we could say in addition to

The database query doesn't actually get run until the queryset is iterated

to emphasise this? As you say, I'm reluctant to repeat the Django docs here, which are fairly clear.

@simkimsia
Copy link
Author

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.

@j4mie
Copy link
Member

j4mie commented Jul 19, 2022

Thanks, I'll consider this. Let's leave the issue open for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants