Distinct can cover most use cases for this, but in general:
MyModel.objects.filter(some_kind_of_manytomany__blah__blah__blah=X).select_related(some_kind_of_manytomany__blah).group_by(some_kind_of_manytomany__blah)

If you tag on anything for an additional select that distinct can't
handle, you need group by.

Btw, your example doesn't work, having happens after the query
finishes executing, not within the where clause. That specific use
case *has* to execute exaclty as shown.
On Mar 24, 6:12 pm, "Russell Keith-Magee" <[EMAIL PROTECTED]>
wrote:
> On Mon, Mar 24, 2008 at 7:19 AM, David Cramer <[EMAIL PROTECTED]> wrote:
>
> >  So we're not going to support group by at all, even though is
> >  extremely simple? I don't understand why everyone says mine (and
> >  Curse's and many other peoples) use of SQL is so uncommon. I'd
> >  guarantee almost every developer and I know who writes any kind of
> >  scalable code (and I've seen what they've written) uses GROUP BY in
> >  SQL much more than just for SUM, or COUNT, or any of those operations.
>
> And yet, the example you give is a count on related objects.
>
> >  SELECT my_table.* FROM my_table JOIN my_other_table WHERE my_other
> >  table.my_table_id = my_table.id GROUP BY my_table.id HAVING COUNT(1) >
> >  1;
>
> >  How do you ever hope to achieve something like this in the ORM without
> >  patches like mine?
>
> Lets see if I can make myself clearer by using your example.
>
> You have provided some sample SQL. However, that wasn't the start
> point. Once upon a time, you had a problem you needed to solve: "I
> need to find all the MyTable objects that have more than one related
> OtherTable objects". Notice that this question doesn't use the phrase
> "GROUP BY".
>
> Django's ORM didn't support aggregates, so you hand crafted some SQL,
> and since aggregates were involved, you needed to use a GROUP BY
> clause. This is because GROUP BY is the way SQL handles aggregation.
>
> Now, we're talking about adding aggregates to Django. So, we need to
> find a way to express your original question as Django syntax. The
> solution to this problem isn't "add GROUP BY to Django Syntax" - it's
> to find an elegant way to represent the underlying problem as an
> object-based syntax. Based on the discussions so far, something like
> this would seem appropriate:
>
> MyTable.objects.aggregate(othertable__count='related_count').filter(related_count__gt=1)
>
> Notice that this query doesn't require the use of the phrase GROUP BY
> in the ORM syntax. When this hits the database, GROUP BY will
> certainly be required. However, the terms for the GROUP BY can be
> entirely implied from the rest of the query. There is no need to
> expose GROUP BY into the ORM syntax.
>
> Your original question wasn't "How do I perform a GROUP BY over a
> MyTable" - it was a real-world question that needed an object-based
> answer. The goal of the Django ORM is to solve your _actual_ problem,
> not your SQL problem.
>
> >  My main argument is there's no reason to *not* include the
> >  functionality except that a few select people say "we dont need it".
>
> There is a very good reason, which I have told you several times. The
> Django ORM does not provide an alternate syntax for SQL. It provides a
> way of interrogating an object model that happens to be backed by a
> database. As a result, the external APIs are not SQL specific - they
> encompass broader object-based use-cases. "Just adding GROUP BY"
> doesn't achieve any of the Django design goals.
>
> Now, if you can present a use case that isn't satisfied by the
> discussions we are currently having - that is, a usage of GROUP BY
> that steps outside the bounds of what could be implied from other
> parts of an ORM query - let us know. We want to be able to cover as
> many potential queries as possible. However, even then, I doubt that
> the solution will be "add GROUP BY" - there will be another class of
> object-based queries that we haven't considered, for which we will
> need a new _object based_ syntax.
>
> Yours,
> Russ Magee %-)
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to django-developers@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-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to