class Packing(models.Model):
tags = models.ManyToManyField(to='PackingTag')
class PackingItem(models.Model):
packing = models.ForeignKey(to='Packing', on_delete=models.CASCADE)
amount = models.DecimalField(max_digits=12, decimal_places=4)
}}}
Consider a queryset like this:
{{{#!python
PackingItem.objects.annotate(tags=Min('packing__tags__title')).values('tags').annotate(total_amount=Sum('amount'))
}}}
Currently on both 4.1 and 3.2 it yields this query using SQLite backend
(MySQL backend on 3.2 outputs a very similar query as well):
{{{
SELECT MIN("packings_packingtag"."title") AS "tags",
CAST(SUM("packings_packingitem"."amount") AS NUMERIC) AS "total_amount"
FROM "packings_packingitem"
INNER JOIN "packings_packing" ON
("packings_packingitem"."packing_id" = "packings_packing"."id")
LEFT OUTER JOIN "packings_packing_tags" ON
("packings_packing"."id" = "packings_packing_tags"."packing_id")
LEFT OUTER JOIN "packings_packingtag" ON
("packings_packing_tags"."packingtag_id" = "packings_packingtag"."id")
}}}
I would expect this queryset to either group by tags (this example may not
make much sense, my use case involves GroupConcat aggregate from
django_mysql package, however, this is the same for all aggregates)
because passing a regular field expression constructs a group by in the
SQL, or crash with an error telling that grouping by annotations is not
supported.
--
Ticket URL: <https://code.djangoproject.com/ticket/34145>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.