Opened 8 years ago
Last modified 8 years ago
#27729 closed New feature
Add a method to evaluate QuerySets. — at Initial Version
Reported by: | AlbinLindskog | Owned by: | Albin Lindskog |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | yes | Needs documentation: | yes |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
I would often like to evaluate a queryset once, i.e. fetch it to local memory, and then repeatedly perform various operations on it, e.g. filtering.
As i currently stands to do that I can either:
- Drop to some native python data structure, e.g. a list, but I find the Django ORM syntax more understandable.
- Not evaluate the QuerySet, but then the performance will take a hit as each operation will query the database.
- I could also force the QuerySet to evaluate, for example by calling len() or bool(), but unless you are familiar with how Django handels QuerySets it's not clear what's being accomplished behind the scenes.
- Lastly I can use the internal method _fetch_all(), which is bad practise. Secondly, because it does not return a QuerySet you can not chain it.
Needless to say, I don't like either option. I think the QuerySet class needs a evaluate()-method, which does, as the name suggest, evaluates the QuerySet.
I've added a patch that accomplishes this. Could I get some feedback on my suggestion and code? If the feedback is positive I'll move to update the documentation and create a pull request.