2
我在想,如果在模型中使用的索引是可能的:Django的查詢集指標
class Buildings(models.Model):
island = models.ForeignKey(Island)
townhall = models.IntegerField(default=1)
lumberjack = models.IntegerField(default=0)
stonequarry = models.IntegerField(default=0)
ironmine = models.IntegerField(default=0)
[...]
a=Buildings.objects.get(somecondition)
print a.townhall # 1
a[1] +=1
print a.townhall # 2
目前,它拋出
"TypeError: 'Buildings' object is unindexable"
爲什麼我想這樣做的原因是,使用索引將簡化如下代碼的部分:
if queue.kind_id == 0: buildings.townhall += 1
elif queue.kind_id == 1: buildings.lumberjack += 1
elif queue.kind_id == 2: buildings.stonequarry += 1
elif queue.kind_id == 3: buildings.ironmine += 1
elif queue.kind_id == 4: buildings.factory += 1
elif queue.kind_id == 5: buildings.shipyard += 1
elif queue.kind_id == 6: buildings.university += 1
對此:
buildings[queue.kind_id] +=1
呃我的錯誤:PI意味着通過索引引用單個對象中的值。使對象[0]引用object.townhall,object [1] - > object.lumberjack等 – Robus 2010-08-08 14:50:28