Access to related Objects inside a model propery
Posted
by aliem
on Stack Overflow
See other posts from Stack Overflow
or by aliem
Published on 2010-04-28T09:47:00Z
Indexed on
2010/04/28
9:53 UTC
Read the original article
Hit count: 151
django-models
Hi, I just run into some problems with django models. Example code is better than any word:
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__()
but, when i call the cart property here's what i get in the python console:
>>> 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
© Stack Overflow or respective owner