Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django. Complex annotations require an alias. What is alias here?

Tags:

django

I'm trying to get the maximun & minimum value of a model with this query:

max_min_price = MyModel.objects.annotate(Min('price', Max('price')))

But I get the error:

Complex annotations require an alias

I'm not sure what an alias means here and the docs are not clear in my opinion. Any advice will help.

like image 613
Alejandro Veintimilla Avatar asked Sep 07 '16 16:09

Alejandro Veintimilla


1 Answers

You need to give a name to the result of Min, since Django wouldn't be able to derive the name for complex aggregate functions:

max_min_price = MyModel.objects.annotate(min_price=Min('price', Max('price')))
like image 106
Rohit Jain Avatar answered Sep 19 '22 11:09

Rohit Jain