Opened 9 years ago
Closed 9 years ago
#27375 closed Bug (duplicate)
Error when using conditional expression in aggregation
| Reported by: | Mehdi Pourfar | Owned by: | nobody |
|---|---|---|---|
| Component: | Database layer (models, ORM) | Version: | 1.10 |
| Severity: | Normal | Keywords: | orm, aggregation, conditional expression |
| Cc: | Triage Stage: | Unreviewed | |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
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.
Note:
See TracTickets
for help on using tickets.
Probably a duplicate of #25307. Please reopen if the patch there doesn't solve the issue, thanks!