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.
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')))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With