Opened 3 years ago

Closed 3 years ago

#27729 closed New feature (wontfix)

Add a method to evaluate QuerySets.

Reported by: AlbinLindskog Owned by: AlbinLindskog
Component: Database layer (models, ORM) Version: master
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 (last modified by AlbinLindskog)

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.

Attachments (1)

evaluate.diff (2.5 KB) - added by AlbinLindskog 3 years ago.

Download all attachments as: .zip

Change History (4)

Changed 3 years ago by AlbinLindskog

Attachment: evaluate.diff added

comment:1 Changed 3 years ago by AlbinLindskog

Description: modified (diff)
Needs documentation: set

comment:2 Changed 3 years ago by AlbinLindskog

Description: modified (diff)
Owner: changed from Albin Lindskog to AlbinLindskog

comment:3 Changed 3 years ago by Tim Graham

Resolution: wontfix
Status: assignedclosed

QuerySet's can't do in-memory filtering. In your test test_evaluate_does_not_query_database, numbers.filter(num__lte=5) can't return the correct results without doing another query. I suspect the test passes because it doesn't evaluate that queryset.

Note: See TracTickets for help on using tickets.
Back to Top