#32548: Support passing conditional expressions to Q().
-------------------------------------+-------------------------------------
     Reporter:  jonathan-golorry     |                    Owner:  jonathan-
         Type:                       |  golorry
  Cleanup/optimization               |                   Status:  assigned
    Component:  Database layer       |                  Version:  dev
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:  Q objects,           |             Triage Stage:  Accepted
  deconstruct                        |
    Has patch:  1                    |      Needs documentation:  0
  Needs tests:  1                    |  Patch needs improvement:  1
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

 * status:  new => assigned
 * cc: Ian Foote, Matthew Schinckel (added)
 * needs_better_patch:  0 => 1
 * needs_tests:  0 => 1
 * owner:  nobody => jonathan-golorry
 * stage:  Unreviewed => Accepted


Comment:

 Conditional expressions can be combined together, so it's not necessary to
 encapsulate `Exists()` with `Q()`. Moreover it's an undocumented and
 untested to pass conditional expressions to `Q()`. Nevertheless I think it
 makes sense to support this.

 There is no need to change the current format of `deconstruct()`, it
 should be enough to handle conditional expressions, e.g.
 {{{
 diff --git a/django/db/models/query_utils.py
 b/django/db/models/query_utils.py
 index ae0f886107..5dc71ad619 100644
 --- a/django/db/models/query_utils.py
 +++ b/django/db/models/query_utils.py
 @@ -85,7 +85,7 @@ class Q(tree.Node):
          if path.startswith('django.db.models.query_utils'):
              path = path.replace('django.db.models.query_utils',
 'django.db.models')
          args, kwargs = (), {}
 -        if len(self.children) == 1 and not isinstance(self.children[0],
 Q):
 +        if len(self.children) == 1 and not isinstance(self.children[0],
 Q) and getattr(self.children[0], 'conditional', False) is False:
              child = self.children[0]
              kwargs = {child[0]: child[1]}
          else:
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/32548#comment:2>
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/074.7da03ab99a820a068c9a4154ac29d429%40djangoproject.com.

Reply via email to