On 8/18/07, Doug B <[EMAIL PROTECTED]> wrote:
>
> > [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().
>
I tried that, but I get the same error, the traceback displays that
line as the "buggy" one: for item in 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
>
Thanks for the tip. I did something like that but then found a snippet
[0] doing what I wanted in a more django-like way. I still don't know
if use my own sql sentences for this cases or stick to the django api.
I guess it's a decision based on performance, the problem is that I
don't know which is more performant.

Each cart won't have many items, so I guess counting them is not going
to be a demanding process, right?

AlvAro

"You can't change the world, but you can change your mind."
Software Libre: El conocimiento no puede tener dueño.

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to