我刚刚遇到django模型的一些问题。 示例代码优于任何单词:
class Cart(models.Model):
updated_at = models.DateTimeField(auto_now=True)
created_at = models.DateTimeField(auto_now_add=True)
def __unicode__(self):
return u'date %s;'%(self.created_at)
def __str__(self):
return self.__unicode__()
def _total_items(self):
""" Totale n di oggetti """
a = 0
for i in self.items.all:
a += i.quantity
return a
total_items = property(_total_items)
class Item(models.Model):
cart = models.ForeignKey(Cart)
quantity = models.PositiveIntegerField()
def __unicode__(self):
return u'product %s'%(self.id)
def __str__(self):
return self.__unicode__()
但是,当我在这里调用cart属性时,我在python控制台中得到了什么:
>>> a.total_items
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "models.py", line 49, in _total_items
for i in self.item_set.all:
TypeError: 'RelatedManager' object is not callable
答案 0 :(得分:3)
尝试替换此行
for i in self.items.all:
这一个
for i in self.items.all():