2016-01-12 27 views
0

在Django視圖中,我運行以下代碼以獲得「位置」列的最大值,其中「groupMain_id」等於10 :Django查詢集過濾器和聚合最大值,然後把最大值放到一個變量中

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

testvar=lastposition 

#Result 
# {'themax': 3} 

如何將值「3」賦值給變量「testvar」? 如果我使用的testvar = lastposition.themax收到錯誤:

Exception Type: AttributeError Exception Value: 'dict' object has no attribute 'themax'

回答

2

lastpositiondict。通過使用lastposition.get('themax', None)或簡單地lastposition['themax']來獲得它的價值。

但是請注意,如果密鑰不存在,後者可以提高KeyError,而get方法則不會。

相關問題