繁体   English   中英

Django queryset过滤器并汇总最大值,然后将最大值放入变量中

[英]Django queryset filter and aggregate max, then put the max value in a variable

在Django视图中,我运行以下代码以获取“ position”列的最大值,其中“ groupMain_id”等于10:

lastposition = BlockMain.objects.filter(groupMain_id = 10).aggregate(themax=Max('position'))

testvar=lastposition

#Result
# {'themax': 3} 

如何为变量“ testvar”分配值“ 3”? 如果我使用testvar = lastposition.themax,则会收到错误消息:

异常类型:AttributeError异常值:'dict'对象没有属性'themax'

lastposition是一个dict 通过使用lastposition.get('themax', None)或仅使用lastposition['themax']来获取其值。

但是请注意,如果Key不存在,则后者会引发KeyError ,而get方法不存在。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM