django-modeltranslation icon indicating copy to clipboard operation
django-modeltranslation copied to clipboard

`MultilingualQueryset` breaks grouping with limit

Open ryselis opened this issue 5 years ago • 0 comments

Consider the following model:

class AdvanceBill(models.Model):
    date = models.DateField()
    total = models.DecimalField(max_digits=12, decimal_places=2)
    number = models.CharField(max_length=10)
    # other fields

    class Meta:
        ordering = ("-date", "-number")

If I try running the following statement:

AdvanceBill.objects.all().values("date").annotate(x_total_sum=Sum(F("total"))).order_by()[:10]

the result is not grouped by values (the same dates have multiple occurences in the result) as the query looks like this (note that values only accept "date" and we have date and number in GROUP BY clause and ORDER BY is applied anyway):

SELECT `accounting_advancebill`.`date`, SUM(`accounting_advancebill`.`total`) AS `x_total_sum` FROM `accounting_advancebill` GROUP BY `accounting_advancebill`.`date`, `accounting_advancebill`.`number` ORDER BY `accounting_advancebill`.`date` DESC, `accounting_advancebill`.`number` DESC LIMIT 10

This happens when the model uses MultilingualQuerySet, (MultilingualManager) if standard QuerySet is used, order_by() removes ordering as expected:

SELECT `accounting_advancebill`.`date`, SUM(`accounting_advancebill`.`total`) AS `x_total_sum` FROM `accounting_advancebill` GROUP BY `accounting_advancebill`.`date` ORDER BY NULL LIMIT 10

ryselis avatar Jun 11 '20 10:06 ryselis