On Fri, Sep 20, 2013 at 7:34 AM, Nicolas Mendoza <hnicolas....@gmail.com>wrote:

> Actually what's the best way for do a group by using django ORM?
>
> I know workarounds like combinate "values" with "Anotate", etc. but I
> wil like know  your favorite workaround without rawSQL and not manager
> based , only Django ORM ;-)
>
> Example.objects.values('type').annotate(dcount=Count('type')) ....
>

The thing is -- your question is wrong, and until you understand *why* it's
wrong, you're going to be frustrated with Django's ORM.

Django's ORM *isn't* SQL. It's an object model. The ORM *very deliberately*
avoids exposing SQL concepts. Asking "How do I do a GROUP BY" is the wrong
question to be asking. There *is* no way to do a GROUP BY in Django's ORM -
and that's by design.

What we *do* have is two things:

Firstly a representation of common object-based summaries, expressed in
object terms. What is the average age of this group of authors? What is the
total sum of the value of all books written by this list of authors.
*These* are object-based queries, not GROUP BY statements. You need to pose
your question in terms of the relationships between objects in order for it
to be meaningful.

Secondly, we have a fall through that will give you access to raw SQL. An
ORM is an abstraction, and all abstractions leak, so sometimes to get the
most efficient representation of a query, you'll need to go to the bare
metal.

So - there's actually no way to answer your question. If you have a
specific query you want to represent, we can help you work out how to
express that in the ORM, but asking "how do I GROUP BY" doesn't make any
sense.

Yours,
Russ Magee %-)

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to