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

I am trying to get the maximum and minimum model value using 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 the alias means here, and the docs are not clear in my opinion. Any advice would help.

+7
django
source share
1 answer

You need to specify a name for the Min result, since Django will not be able to get a name for complex aggregate functions:

 max_min_price = MyModel.objects.annotate(min_price=Min('price', Max('price'))) 
+7
source share

All Articles