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

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/051.4598ec1cd05e7d88143ff7166ccd6dbc%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to