[Django] #26517: Empty Querset Using Empty Queryset in ExpressionWrapper

9 views
Skip to first unread message

Django

unread,
Apr 19, 2016, 6:36:34 AM4/19/16
to django-...@googlegroups.com
#26517: Empty Querset Using Empty Queryset in ExpressionWrapper
----------------------------------------------+--------------------
Reporter: srkunze | Owner: nobody
Type: Bug | Status: new
Component: Database layer (models, ORM) | Version: 1.8
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------------------+--------------------
We use the ExpressionWrapper + annotate to annotate specific boolean
values to all queried instances like this (minimal working example):

{{{
editable_ones = ..... # another queryset
result =
MyModel.objects.annotate(editable=ExpressionWrapper(Q(pk__in=editable_child_folders),
output_field=BooleanField())
}}}

As soon as {{{editable_ones}}} is empty, {{{result}}} is also empty.
Reading the docs, I guess this is not desired behavior
(https://docs.djangoproject.com/en/1.9/ref/models/querysets/#annotate).

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

Django

unread,
Apr 19, 2016, 6:39:26 AM4/19/16
to django-...@googlegroups.com
#26517: Empty Querset Using Empty Queryset in ExpressionWrapper
-------------------------------------+-------------------------------------

Reporter: srkunze | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by srkunze):

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


Old description:

> We use the ExpressionWrapper + annotate to annotate specific boolean
> values to all queried instances like this (minimal working example):
>
> {{{
> editable_ones = ..... # another queryset
> result =
> MyModel.objects.annotate(editable=ExpressionWrapper(Q(pk__in=editable_child_folders),
> output_field=BooleanField())
> }}}
>
> As soon as {{{editable_ones}}} is empty, {{{result}}} is also empty.
> Reading the docs, I guess this is not desired behavior
> (https://docs.djangoproject.com/en/1.9/ref/models/querysets/#annotate).

New description:

We use the ExpressionWrapper + annotate to annotate specific boolean
values to all queried instances like this (minimal working example):

{{{
editable_ones = ..... # another queryset
result =

MyModel.objects.annotate(editable=ExpressionWrapper(Q(pk__in=editable_ones),
output_field=BooleanField())
}}}

As soon as {{{editable_ones}}} is empty, {{{result}}} is also empty.
Reading the docs, I guess this is not desired behavior
(https://docs.djangoproject.com/en/1.9/ref/models/querysets/#annotate).

--

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

Django

unread,
Apr 19, 2016, 8:41:53 AM4/19/16
to django-...@googlegroups.com
#26517: Empty Querset Using Empty Queryset in ExpressionWrapper
-------------------------------------+-------------------------------------

Reporter: srkunze | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by srkunze):

I did some further analysis:

{{{
MyModel.objects.annotate(editable=ExpressionWrapper(Q(pk__in=MyModel.objects.none()),
output_field=BooleanField())
}}}

{{{
MyModel.objects.annotate(editable=ExpressionWrapper(Q(pk__in=MyModel.objects.filter(pk=-1)),
output_field=BooleanField())
}}}


Both results differ.

{{{MyModel.objects.none()}}} forces the outer query to return an empty
QuerySet.

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

Django

unread,
Apr 19, 2016, 9:51:45 AM4/19/16
to django-...@googlegroups.com
#26517: Empty Querset Using Empty Queryset in ExpressionWrapper
-------------------------------------+-------------------------------------

Reporter: srkunze | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: master

(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by charettes):

* version: 1.8 => master
* stage: Unreviewed => Accepted


Comment:

Related to or duplicate of both #26430 and #26433.

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

Django

unread,
May 22, 2016, 12:24:29 PM5/22/16
to django-...@googlegroups.com
#26517: Empty Querset Using Empty Queryset in ExpressionWrapper
-------------------------------------+-------------------------------------

Reporter: srkunze | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by charettes):

* has_patch: 0 => 1


Comment:

[https://github.com/django/django/pull/6638 PR]

--
Ticket URL: <https://code.djangoproject.com/ticket/26517#comment:4>

Django

unread,
May 22, 2016, 7:40:41 PM5/22/16
to django-...@googlegroups.com
#26517: Empty Querset Using Empty Queryset in ExpressionWrapper
-------------------------------------+-------------------------------------

Reporter: srkunze | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by jarshwah):

FYI ExpressionWrapper was never meant to be used to wrap up Q() objects.
Q() are not really expressions themselves, they just implement some of the
same API internally.

You're much better off using Case Expressions
https://docs.djangoproject.com/en/1.9/ref/models/conditional-expressions/
for this use case:

{{{
MyModel.objects.annotate(editable=Case(When(pk__in=[editable], then=1),
default=0, output_field=BooleanField()) )
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/26517#comment:5>

Django

unread,
Jun 28, 2016, 10:59:04 AM6/28/16
to django-...@googlegroups.com
#26517: Empty Querset Using Empty Queryset in ExpressionWrapper
-------------------------------------+-------------------------------------

Reporter: srkunze | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* needs_better_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/26517#comment:6>

Django

unread,
Jul 10, 2016, 8:01:16 AM7/10/16
to django-...@googlegroups.com
#26517: Empty Querset Using Empty Queryset in ExpressionWrapper
-------------------------------------+-------------------------------------

Reporter: srkunze | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by emulbreh):

* needs_better_patch: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/26517#comment:7>

Django

unread,
Aug 5, 2016, 10:22:39 AM8/5/16
to django-...@googlegroups.com
#26517: Empty Querset Using Empty Queryset in ExpressionWrapper
-------------------------------------+-------------------------------------

Reporter: srkunze | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/26517#comment:8>

Django

unread,
Aug 8, 2016, 11:21:28 AM8/8/16
to django-...@googlegroups.com
#26517: Empty Querset Using Empty Queryset in ExpressionWrapper
-------------------------------------+-------------------------------------
Reporter: srkunze | Owner: nobody
Type: Bug | Status: closed

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

Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham <timograham@…>):

* status: new => closed
* resolution: => fixed


Comment:

In [changeset:"c002a0d39f18694f5e8a07d86684fc793b063056" c002a0d3]:
{{{
#!CommitTicketReference repository=""
revision="c002a0d39f18694f5e8a07d86684fc793b063056"
Fixed #26517 -- Fixed ExpressionWrapper with empty queryset.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/26517#comment:9>

Reply all
Reply to author
Forward
0 new messages