On Mon, Aug 3, 2009 at 11:17 PM, Javier Guerra<[email protected]> wrote:
>
> Hi all,
>
>
> I've just upgraded to 1.1 (congrats everybody!), and want to put the
> aggregation/annotation features to use. but it doesn't seem to be a
> way to annotate the records with counts of different subsets of
> related records.
>
> my problem: i have 'Order' and 'Item' models, and the items can be
> on several states or processing:
>
> class Order (models.Model):
> order_no = models.Integerfield()
> date = models.DateTimeField()
> client = models.ForeignKey (Client)
>
> class Item (models.Model):
> order = models.ForeignKey (Order)
> description = models.CharField (max_length=50)
> price = models.DecimalField (max_digits=7, decimal_places=2)
> quantity = models.IntegerField()
> status = models.SmallIntegerField()
>
> and I want to show a list of all the orders, with columns showing how
> many items are on each state:
>
> | order | client | not yet | processing | ready |
> --------------------------------------------------------
> | 000001 | Jon Doe | 5 | 4 | 0 |
> | 000002 | Jane Smith | 2 | 7 | 3 |
> --------------------------------------------------------
>
> so far, i only found how to add a column with the total number of items:
>
> Order.objects.annotate(Count('item'))
>
> but i can't count subsets of items. Is there any way to do that in
> the DB, or I have to do it in Python?
You currently cannot do this in one step, but maybe something like
this might give you ideas:
# orders is a queryset or a list of Order objects
Item.objects.filter(order__in=orders).values('order',
'status').annotate(Sum('quantity'))
... and then you post-process the returned values in some way. At
least you can use the database capabilities for a significant part of
what you are interested in.
Matthias
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---