【发布时间】:2015-01-02 23:50:17
【问题描述】:
我有一个具有 IntegerField 命名为阈值的模型。 无论负值如何,我都需要获得阈值的总 SUM。
vote_threshold
100
-200
-5
result = 305
现在我就是这样做的。
earning = 0
result = Vote.objects.all().values('vote_threshold')
for v in result:
if v.vote_threshold > 0:
earning += v.vote_threshold
else:
earning -= v.vote_threshold
什么是更快更合适的方法?
【问题讨论】:
-
为什么用
earning变量加减vote_threshold!!? -
我需要结果为阳性。 +10 , -10 = 20
-
检查我编辑的答案。