[Django] #27729: Add a method to evaluate QuerySets.

25 views
Skip to first unread message

Django

unread,
Jan 12, 2017, 11:17:33 AM1/12/17
to django-...@googlegroups.com
#27729: Add a method to evaluate QuerySets.
-------------------------------------+-------------------------------------
Reporter: | Owner: Albin Lindskog
AlbinLindskog |
Type: New | Status: assigned
feature |
Component: Database | Version: master
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 1
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
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.

--
Ticket URL: <https://code.djangoproject.com/ticket/27729>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jan 12, 2017, 11:17:47 AM1/12/17
to django-...@googlegroups.com
#27729: Add a method to evaluate QuerySets.
-------------------------------------+-------------------------------------
Reporter: AlbinLindskog | Owner: Albin
| Lindskog
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by AlbinLindskog):

* Attachment "evaluate.diff" added.

Django

unread,
Jan 12, 2017, 11:21:26 AM1/12/17
to django-...@googlegroups.com
#27729: Add a method to evaluate QuerySets.
-------------------------------------+-------------------------------------
Reporter: AlbinLindskog | Owner: Albin
| Lindskog
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 1

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by AlbinLindskog):

* needs_docs: 0 => 1


Old 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.

New 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.

I should clarify that I have assign this ticket to myself.

--

--
Ticket URL: <https://code.djangoproject.com/ticket/27729#comment:1>

Django

unread,
Jan 12, 2017, 11:24:53 AM1/12/17
to django-...@googlegroups.com
#27729: Add a method to evaluate QuerySets.
-------------------------------------+-------------------------------------
Reporter: AlbinLindskog | Owner:
| AlbinLindskog

Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 1
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by AlbinLindskog):

* owner: Albin Lindskog => AlbinLindskog


Old 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.
>

> I should clarify that I have assign this ticket to myself.

New 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.

--

--
Ticket URL: <https://code.djangoproject.com/ticket/27729#comment:2>

Django

unread,
Jan 12, 2017, 11:46:44 AM1/12/17
to django-...@googlegroups.com
#27729: Add a method to evaluate QuerySets.
-------------------------------------+-------------------------------------
Reporter: AlbinLindskog | Owner:
| AlbinLindskog
Type: New feature | Status: closed

Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution: wontfix

Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 1
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* status: assigned => closed
* resolution: => wontfix


Comment:

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.

--
Ticket URL: <https://code.djangoproject.com/ticket/27729#comment:3>

Reply all
Reply to author
Forward
0 new messages