thanks.

this is good enough, although in general having to fall back to sql is
not portable between different database engines.

i think that at least having methods that return the names of the
underlying database tables and/or fields would be helpful. so that i
could use them in stead of 'myapp_a_bs' or 'a_id'. just in case the
naming scheme changes later. i realise of course that i could go the
other way and make the models use my own names when creating the
tables/fields in the first place.

i appreciate your help
konstantin

On Feb 18, 3:31 pm, "Honza Král" <[EMAIL PROTECTED]> wrote:
> 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- Hide quoted text -
>
> - Show quoted text -


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