Hi,
I believe you could use
queryset.extra( select={ 'B_count' : 'SELECT COUNT(*) FROM myapp_a_bs
WHERE a_id = myapp_a.id' } )
it will result in a slightly different, but equivalent, query:
SELECT
myapp_a.id,
myapp_a,
...,
( SELECT COUNT(*) FROM myapp_a_bs WHERE a_id = myapp_a.id ) AS "B_count"
FROM
...
or you could call
.b_set.count() on every a object you would get (one query for every
instance !! )
there is no clean (without SQL) way to do aggregations in django ORM.
On 2/18/07, akonsu <[EMAIL PROTECTED]> wrote:
>
> hello,
>
> class A(models.Model):
> ....
> bs = models.ManyToManyField('B')
>
> class B(models.Model):
> ....
>
> how to implement this query:
>
> select *
> from myapp_a
> inner join (select a_id, count(*)
> from myapp_a_bs
> group by a_id) as a
> on myapp_a.id=a.a_id;
>
> thanks for any help!
> konstantin
>
>
> >
>
--
Honza Kr�l
E-Mail: [EMAIL PROTECTED]
ICQ#: 107471613
Phone: +420 606 678585
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---