views:

49

answers:

1

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
A: 

Try replacing this line

for i in self.items.all:

with this one

for i in self.items.all():
Mathieu Steele
:) it was my own first answer to my prob, btw it seems that the RelatedManager was not ready when I was calling it
aliem