【发布时间】:2016-03-06 16:58:29
【问题描述】:
我创建列表的代码是:
choices = []
for bet in Bet.objects.all():
#...
#Here is code that skip loop if bet.choice exist in choices[]
#...
temp = {
'choice':bet.choice,
'amount':bet.sum,
'count':bets.filter(choice=bet.choice).count()}
choices.append(temp)
choices.sort(key=attrgetter('choice'), reverse=True)
choices.sort(key=attrgetter('amount'), reverse=True)
choices.sort(key=attrgetter('count'), reverse=True)
我必须按列表排序,因为模型 orderby() 不能按 count() 排序,可以吗?
【问题讨论】:
-
请发布(并阅读)完整的错误。
-
@skyking:
AttributeError: 'dict' object has no attribute 'choice'.. -
这意味着
dict对象没有choice属性。实际上阅读错误消息并考虑一下是排除故障时的良好开端。事实证明,缺少该属性的是列表的元素。
标签: python list sorting dictionary