[Django] #27021: Add explicit support for Q object annotations

19 views
Skip to first unread message

Django

unread,
Aug 4, 2016, 8:39:18 PM8/4/16
to django-...@googlegroups.com
#27021: Add explicit support for Q object annotations
-------------------------------------+-------------------------------------
Reporter: jarshwah | Owner: nobody
Type: New | Status: new
feature |
Component: Database | Version: master
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: Accepted | Has patch: 0
Needs documentation: 1 | Needs tests: 1
Patch needs improvement: 1 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
A common pattern that has arisen with expressions is to annotate a Q
object to the query without going through the Case/When ceremony.

{{{
Model.objects.annotate(boolfield=ExpressionWrapper(Q(field__gte=4),
output_field=BooleanField()))
}}}

We've tried to discourage this pattern because the Case/When structure is
a lot more powerful and correct. Sometimes the power isn't required and
users really just want to annotate a boolean expression. Q objects and the
WhereNode it resolves to aren't really proper expressions, but they could
be. Case/When uses Q objects internally, which is why it's important to
test Case/When for any major changes to Q and WhereNode.

Explicit support for Q object annotations should implement an implicit
BooleanField as the output_field. I believe this is only required on the
WhereNode since that is what is ultimately resolved. Once this happens,
the above query becomes a lot nicer.

{{{
Model.objects.annotate(boolfield=Q(field__gte=4))
}}}

For this to work the following is definitely required:

- Add an `output_field=BooleanField()` to `WhereNode`
- Add a `resolve_expression()` to `WhereNode` (unsure what this should
return - requires significant testing with Case/When)

Might be required:

- Add an `output_field=BooleanField()` to `Q`

Tests:

- Adding a subquery containing a Q annotation to a parent query
- Adding a subquery containing a Case/When annotation to a parent query
(if it's not already tested)
- Standard tests that most other annotations already implement
- Nullable fields used in Q annotations

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

Django

unread,
Aug 15, 2016, 11:53:54 PM8/15/16
to django-...@googlegroups.com
#27021: Add explicit support for Q object annotations
-------------------------------------+-------------------------------------
Reporter: jarshwah | Owner: Ian-Foote
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:

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

* owner: nobody => Ian-Foote
* status: new => assigned


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

Django

unread,
Aug 16, 2016, 12:24:54 AM8/16/16
to django-...@googlegroups.com
#27021: Add explicit support for Q object annotations
-------------------------------------+-------------------------------------
Reporter: jarshwah | Owner: Ian-Foote
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 1
Needs tests: 1 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Ian-Foote):

I took a quick look at this and ran into circular import problems when
setting {{{output_field=BooleanField()}}} or importing {{{Expression}}}
(both from {{{django.db.models}}}).

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

Django

unread,
Aug 16, 2016, 12:25:02 AM8/16/16
to django-...@googlegroups.com
#27021: Add explicit support for Q object annotations
-------------------------------------+-------------------------------------
Reporter: jarshwah | Owner:
Type: New feature | Status: new

Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 1
Needs tests: 1 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Ian-Foote):

* status: assigned => new
* owner: Ian-Foote =>


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

Django

unread,
Dec 2, 2017, 1:56:27 AM12/2/17
to django-...@googlegroups.com
#27021: Add explicit support for Q object annotations
-------------------------------------+-------------------------------------
Reporter: Josh Smeaton | Owner: Sergey
| Fedoseev

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

* owner: (none) => Sergey Fedoseev


* status: new => assigned


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

Django

unread,
Dec 28, 2018, 9:21:55 PM12/28/18
to django-...@googlegroups.com
#27021: Add explicit support for Q object annotations
-------------------------------------+-------------------------------------
Reporter: Josh Smeaton | Owner: Sergey
| Fedoseev
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 1
Needs tests: 1 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Ryan Hiebert):

* cc: Ryan Hiebert (added)


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

Django

unread,
Nov 6, 2019, 11:52:26 AM11/6/19
to django-...@googlegroups.com
#27021: Add explicit support for Q object annotations
-------------------------------------+-------------------------------------
Reporter: Josh Smeaton | Owner: Sergey
| Fedoseev
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 1
Needs tests: 1 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Balazs Endresz):

* cc: Balazs Endresz (added)


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

Django

unread,
Nov 10, 2019, 9:19:39 AM11/10/19
to django-...@googlegroups.com
#27021: Add explicit support for Q object annotations
-------------------------------------+-------------------------------------
Reporter: Josh Smeaton | Owner: Sergey
| Fedoseev
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 1
Needs tests: 1 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Matthijs Kooijman):

* cc: Matthijs Kooijman (added)


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

Django

unread,
Nov 25, 2020, 5:41:00 AM11/25/20
to django-...@googlegroups.com
#27021: Add explicit support for Q object annotations
-------------------------------------+-------------------------------------
Reporter: Josh Smeaton | Owner: Sergey
| Fedoseev
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 1
Needs tests: 1 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Petr Přikryl):

* cc: Petr Přikryl (added)


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

Django

unread,
Nov 28, 2020, 2:32:05 PM11/28/20
to django-...@googlegroups.com
#27021: Add explicit support for Q object annotations
-------------------------------------+-------------------------------------
Reporter: Josh Smeaton | Owner: (none)

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

* owner: Sergey Fedoseev => (none)


* status: assigned => new


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

Django

unread,
Apr 2, 2021, 8:02:38 PM4/2/21
to django-...@googlegroups.com
#27021: Add explicit support for Q object annotations
-------------------------------------+-------------------------------------
Reporter: Josh Smeaton | Owner: (none)
Type: New feature | Status: new
Component: Database layer | Version: dev

(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 1
Needs tests: 1 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Ian Foote):

While working on #470, I've created a proof of concept implementation that
allows annotating lookups without requiring a {{{Q}}} object. (See
https://github.com/django/django/commit/d67c858198903839b6b5bf15e04cbf7a7072190e.)

Instead of {{{Model.objects.annotate(boolfield=Q(field__gte=4))}}}, we
would instead write something like
{{{Model.objects.annotate(boolfield=F('field') >= 4)}}}.

This is more flexible than the original proposal here because it allows
comparing expressions other than fields. For example:
{{{Model.objects.annotate(boolfield=Random() > 0.5)}}}.

If this proposed API is accepted, I'll assign this ticket to myself again
and polish up my proof of concept.

--
Ticket URL: <https://code.djangoproject.com/ticket/27021#comment:10>

Django

unread,
Apr 2, 2021, 8:02:47 PM4/2/21
to django-...@googlegroups.com
#27021: Add explicit support for Q object annotations
-------------------------------------+-------------------------------------
Reporter: Josh Smeaton | Owner: (none)
Type: New feature | Status: new
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 1
Needs tests: 1 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Ian Foote):

* cc: Ian Foote (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/27021#comment:11>

Django

unread,
Apr 3, 2021, 3:24:42 AM4/3/21
to django-...@googlegroups.com
#27021: Add explicit support for Q object annotations
-------------------------------------+-------------------------------------
Reporter: Josh Smeaton | Owner: Ian Foote

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

* owner: (none) => Ian Foote


* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/27021#comment:12>

Django

unread,
Jun 5, 2021, 1:44:26 PM6/5/21
to django-...@googlegroups.com
#27021: Add explicit support for Q object annotations
-------------------------------------+-------------------------------------
Reporter: Josh Smeaton | Owner: Ian Foote
Type: New feature | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 1

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

* has_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/27021#comment:13>

Django

unread,
Jun 25, 2021, 3:47:21 AM6/25/21
to django-...@googlegroups.com
#27021: Add explicit support for Q object annotations
-------------------------------------+-------------------------------------
Reporter: Josh Smeaton | Owner: Ian Foote
Type: New feature | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

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

* needs_docs: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/27021#comment:14>

Django

unread,
Jul 8, 2021, 7:15:09 AM7/8/21
to django-...@googlegroups.com
#27021: Add explicit support for Q object annotations
-------------------------------------+-------------------------------------
Reporter: Josh Smeaton | Owner: Ian Foote
Type: New feature | Status: assigned
Component: Database layer | Version: dev
(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 Mariusz Felisiak):

* needs_tests: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/27021#comment:15>

Django

unread,
Jul 9, 2021, 3:32:08 AM7/9/21
to django-...@googlegroups.com
#27021: Add explicit support for Q object annotations
-------------------------------------+-------------------------------------
Reporter: Josh Smeaton | Owner: Ian Foote
Type: New feature | Status: assigned
Component: Database layer | Version: dev
(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 Mariusz Felisiak):

* needs_better_patch: 1 => 0
* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/27021#comment:16>

Django

unread,
Jul 9, 2021, 7:16:25 AM7/9/21
to django-...@googlegroups.com
#27021: Add explicit support for Q object annotations
-------------------------------------+-------------------------------------
Reporter: Josh Smeaton | Owner: Ian Foote
Type: New feature | Status: closed

Component: Database layer | Version: dev
(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 Mariusz Felisiak <felisiak.mariusz@…>):

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


Comment:

In [changeset:"f42ccdd835e5b3f0914b5e6f87621c648136ea36" f42ccdd8]:
{{{
#!CommitTicketReference repository=""
revision="f42ccdd835e5b3f0914b5e6f87621c648136ea36"
Fixed #27021 -- Allowed lookup expressions in annotations, aggregations,
and QuerySet.filter().

Thanks Hannes Ljungberg and Simon Charette for reviews.

Co-authored-by: Mariusz Felisiak <felisiak...@gmail.com>
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/27021#comment:17>

Reply all
Reply to author
Forward
0 new messages