﻿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
27375	Error when using conditional expression in aggregation	Mehdi Pourfar	nobody	"I'm trying to use conditional expression in aggregation, but I encounter with the following error:


{{{
AttributeError: 'WhereNode' object has no attribute 'get_source_expressions'
}}}

This is my model:

{{{
class Rate(models.Model):
    date = models.DateField()                                                                                                                                               
    metal = models.CharField(max_length=6)                                                                                                                       
    location = models.CharField(max_length=6)                                                                                                                                           
    local_cash_settlement = models.DecimalField()
    exchange_rate = models.DecimalField()                                                                                                                              
    three_month_usd = models.DecimalField( )
}}}
 
And here is my queryset:

{{{
Rate.objects.annotate(
    usd_cash_settlement=Case(
        When(
            location=""usa"",
            then=F('local_cash_settlement')
        ),
        default=F('exchange_rate') * F('local_cash_settlement'),
        output_field=models.DecimalField()
    ),
).aggregate(
    first=Avg('usd_cash_settlement'),
    second=Avg(
        Case(
            When(
                location=""usa"",
                then=F('three_month_usd'),
            ),
            default=F('local_cash_settlement'),
            output_field=models.DecimalField()
        )
    )
)
}}}
This is the second aggregation in my queryset that causes this error. 
If I remove it, everything works fine but I can't understand why."	Bug	closed	Database layer (models, ORM)	1.10	Normal	duplicate	orm, aggregation, conditional expression		Unreviewed	0	0	0	0	0	0
