[英]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.