{{{
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.
* owner: nobody => Ian-Foote
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/27021#comment:1>
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>
* status: assigned => new
* owner: Ian-Foote =>
--
Ticket URL: <https://code.djangoproject.com/ticket/27021#comment:3>
* owner: (none) => Sergey Fedoseev
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/27021#comment:4>
* cc: Ryan Hiebert (added)
--
Ticket URL: <https://code.djangoproject.com/ticket/27021#comment:5>
* cc: Balazs Endresz (added)
--
Ticket URL: <https://code.djangoproject.com/ticket/27021#comment:6>
* cc: Matthijs Kooijman (added)
--
Ticket URL: <https://code.djangoproject.com/ticket/27021#comment:7>
* cc: Petr Přikryl (added)
--
Ticket URL: <https://code.djangoproject.com/ticket/27021#comment:8>
* owner: Sergey Fedoseev => (none)
* status: assigned => new
--
Ticket URL: <https://code.djangoproject.com/ticket/27021#comment:9>
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>
* cc: Ian Foote (added)
--
Ticket URL: <https://code.djangoproject.com/ticket/27021#comment:11>
* owner: (none) => Ian Foote
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/27021#comment:12>
* has_patch: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/27021#comment:13>
* needs_docs: 1 => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/27021#comment:14>
* needs_tests: 1 => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/27021#comment:15>
* needs_better_patch: 1 => 0
* stage: Accepted => Ready for checkin
--
Ticket URL: <https://code.djangoproject.com/ticket/27021#comment:16>
* 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>