On Tue, Aug 4, 2009 at 5:17 AM, 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?

Yes, there is a way to do this in the database - but no, there isn't a
way to use Django's aggregation features to expose the capability.

To achieve this in SQL, you need to add an inner query for each
summary column you want; each summary column filters the result set to
the subset of items you want; then join those inner tables with the
master results table.

Yours,
Russ Magee %-)

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

Reply via email to