class Author(models.Model):
name = models.CharField(max_length=200)
class Entry(models.Model):
headline = models.CharField(max_length=255)
authors = models.ManyToManyField(Author)
def __str__(self):
return self.headline
class Meta:
ordering = [
models.Min('authors__id').asc(),
models.Max('authors__id').asc()
]
}}}
The query to get entries will fail with a {{{ProgrammingError}}}: the SQL
query doesn't contain the {{{GROUP BY}}} clause.
As aggregation functions are QueryExpression objects, and the ordering
field handles QueryExpressions since Django 2.0, it should work.
--
Ticket URL: <https://code.djangoproject.com/ticket/29539>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* cc: wilhelmhb (added)
--
Ticket URL: <https://code.djangoproject.com/ticket/29539#comment:1>
* component: Uncategorized => Database layer (models, ORM)
* stage: Unreviewed => Accepted
Comment:
I'm not sure how this should be solved. The problem is that adding that
`order_by()` clause to a model without `Meta.ordering` doesn't work:
{{{
>>> Entry.objects.order_by(models.Min('authors__id').asc(),
models.Max('authors__id').asc()))
...
django.core.exceptions.FieldError: Using an aggregate in order_by()
without also including it in annotate() is not allowed:
OrderBy(Min(F(authors__id)), descending=False)
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/29539#comment:2>
* cc: Can Sarıgöl (added)
* has_patch: 0 => 1
Comment:
I tried to fix this. Could you review?
[https://github.com/django/django/pull/11111 PR]
--
Ticket URL: <https://code.djangoproject.com/ticket/29539#comment:3>
* needs_better_patch: 0 => 1
Comment:
Removing the deprecation from #14357 isn't appropriate.
--
Ticket URL: <https://code.djangoproject.com/ticket/29539#comment:4>
* needs_better_patch: 1 => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/29539#comment:5>
* status: new => closed
* resolution: => wontfix
Comment:
From Simon on the pull request:
I think this feature would be hard to get right for the rare case where
it's useful. It also go against our stance on trying to disconnect
Meta.ordering from aggregation (#14357) as this PR demonstrates. I'd be in
favour of won't fixing the ticket as well for these reasons. It feels
incoherent with the ongoing deprecation motivated by observed user
confusion over the years from the interactions between these two features.
--
Ticket URL: <https://code.djangoproject.com/ticket/29539#comment:6>