Python Django queryset筛选并聚合最大值,然后将最大值放入变量中

Python Django queryset筛选并聚合最大值,然后将最大值放入变量中,python,django,Python,Django,在Django视图中,我运行以下代码以获得“position”列的最大值,其中“groupMain_id”等于10: lastposition = BlockMain.objects.filter(groupMain_id = 10).aggregate(themax=Max('position')) testvar=lastposition #Result # {'themax': 3} 如何将值“3”分配给变量“testvar”? 如果我使用testvar=lastposition.

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

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

testvar=lastposition

#Result
# {'themax': 3} 
如何将值“3”分配给变量“testvar”? 如果我使用testvar=lastposition.themax,我将收到错误:

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


lastposition
是一个
dict
。使用
lastposition.Get('themax',None)
或简单地
lastposition['themax']
获取其值


但是,请注意,如果密钥不存在,后者可能会引发
KeyError
,而
get
方法不存在。

lastposition
是一个
dict
。使用
lastposition.Get('themax',None)
或简单地
lastposition['themax']
获取其值

但是,请注意,如果密钥不存在,则后者可能会引发
KeyError
,而
get
方法则不存在