#24420: Can't order_by annotated field if annotated field uses conditional
expression
-------------------------------------+-------------------------------------
     Reporter:  krisfields           |                    Owner:  jarshwah
         Type:  Bug                  |                   Status:  assigned
    Component:  Uncategorized        |                  Version:  1.8beta1
     Severity:  Normal               |               Resolution:
     Keywords:  order_by,            |             Triage Stage:  Accepted
  annotation, conditional            |
  expression                         |
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by jarshwah):

 See: https://github.com/django/django/pull/4239

 Funnily enough, being explicit worked before this patch:

 CaseTestModel.objects.filter(integer__lte=2).annotate(test=Case(
                 When(integer=1, then=2),
                 When(integer=2, then=1),
                 default=3,
                 output_field=models.IntegerField(),
             )).order_by(F('test').asc())

 The problem is that the same instance of each `When` clause has
 resolve_expression called on it multiple times. Once for the annotation,
 and once again for the order_by. The problem is that
 When.resolve_expression converts the Q() into a WhereNode(), and
 overwrites the condition property. It's not idempotent.

--
Ticket URL: <https://code.djangoproject.com/ticket/24420#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 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/068.5ff49c68fad4e3b399582669cb5499e5%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to