> [PYTHON] > class Order(models.Model): > def count_items(self): > items = 0 > for item in self.orderitem_set.filter(order=self): > items += item.quantity > return items > count = property(count_items) > [/PYTHON]
For the instance.othermodel_set objects, the 'order=self' is already implied, you shouldn't supply it as a filter argument. I have a feeling the error is from specifying the same column twice. Try self.orderitem_set.all(). You might also consider dropping to sql for stuff like this if you've got performance issues. Probably overkill here, but since I had to spend some time figuring it out on a similar problem today, maybe it will help you. from django.db import connection c=connection.cursor() q="select SUM(quantity) as item_count from %s where order=%s" % (self.orderitem_set.model._meta.db_table,self._get_pk_val()) c.execute(q) count = c.fetchone()[0] # grab the first and only row --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---