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

Avoid redundant use of .all() #520

Open
UnknownPlatypus opened this issue Dec 30, 2024 · 0 comments
Open

Avoid redundant use of .all() #520

UnknownPlatypus opened this issue Dec 30, 2024 · 0 comments

Comments

@UnknownPlatypus
Copy link
Contributor

Description

This is inspired by one of your blog posts (which I enjoy reading btw, keep it up!).

Basically it would remove any .all() call that is not the last one (except if there is a .delete())

-Digger.objects.all().filter(height_cm__gt=200)
+Digger.objects.filter(height_cm__gt=200)

Digger.objects.all().delete() # Unchanged, since the `.all()` is required in this specific case

This will require some heuristics to know if we are actually checking a queryset chain but It should be doable (First element of the call chain imported from models, .all() following an objects and before any django queryset known methods, ...).

I'll be quoting youself for the benefits which are not huge but still nice to have I think.

Useless calls to .all() aren’t a large problem, but they do mean more code to read and a slight performance cost for the extra queryset copies. I think that avoiding them also shows you understand ORM methods a little bit better.

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

1 participant