﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
24420	Can't order_by annotated field if annotated field uses conditional expression	Kris Fields	Josh Smeaton	"Continuing with the Client example on https://docs.djangoproject.com/en/dev/ref/models/conditional-expressions/


{{{
from django.db import models

class Client(models.Model):
    REGULAR = 'R'
    GOLD = 'G'
    PLATINUM = 'P'
    ACCOUNT_TYPE_CHOICES = (
        (REGULAR, 'Regular'),
        (GOLD, 'Gold'),
        (PLATINUM, 'Platinum'),
    )
    name = models.CharField(max_length=50)
    registered_on = models.DateField()
    account_type = models.CharField(
        max_length=1,
        choices=ACCOUNT_TYPE_CHOICES,
        default=REGULAR,
    )

Client.objects.annotate(
    discount=Case(
        When(account_type=Client.GOLD, then=Value('5%')),
        When(account_type=Client.PLATINUM, then=Value('10%')),
        default=Value('0%'),
        output_field=CharField(),
        ),
    ).order_by('discount')
}}}

Error is:

{{{
<repr(<django.db.models.query.QuerySet at 0x10afaef10>) failed: AttributeError: 'WhereNode' object has no attribute 'resolve_expression'>
}}}
"	Bug	closed	Database layer (models, ORM)	1.8beta1	Normal	fixed	order_by, annotation, conditional expression		Ready for checkin	1	0	0	0	0	0
