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

匿名 (未验证) 提交于 2019-12-03 08:44:33

问题:

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.

回答1:

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'))) 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!