#24385: Sum() doesn't seem to respect .distinct() in the queryset filter before .aggregate() when filtering by m2m. -------------------------------------+------------------------------------- Reporter: mcagl | Owner: nobody Type: Uncategorized | Status: new Component: Database layer | Version: 1.7 (models, ORM) | Severity: Normal | Resolution: Keywords: | Triage Stage: | Unreviewed Has patch: 0 | Needs documentation: 0 Needs tests: 0 | Patch needs improvement: 0 Easy pickings: 0 | UI/UX: 0 -------------------------------------+------------------------------------- Changes (by mcagl):
* needs_better_patch: => 0 * needs_tests: => 0 * version: 1.6 => 1.7 * needs_docs: => 0 Old description: > Hello, > I setup a minimal project here: > https://github.com/mcagl/minimal_django_sum_test to demonstrate the > problem. > > I'm working with Django 1.6.x and I noticed something that I don't > understand. > > As you can see from the github repository, I have a Tag model and a Row > model with a m2m towards Tag and a DecimalField called amount. > > If I filter Row objects for more than one Tag, and there is/are Row > objects that have more than one Tag among the one filtered by, > Sum('amount') counts it/them once per Tag, even if I use distinct(). > > Please note also that I assert, in the test, that the filtered queryset > is composed by three Row objects, as expected, but the next assert fails, > with 40 != 30. > > I added a test that instead of aggregate(Sum('amount')) does > sum([x.amount for x in rows]) which passes. > > Is this a bug in Sum() or am I missing something? > > Kind regards, > Mark New description: Hello, I setup a minimal project here: https://github.com/mcagl/minimal_django_sum_test to demonstrate the problem. I'm working with Django 1.6.x and I noticed something that I don't understand. Edit: I checked also with the last stable version, 1.7.4 downloaded right from the website, and the problem is still there. As you can see from the github repository, I have a Tag model and a Row model with a m2m towards Tag and a DecimalField called amount. If I filter Row objects for more than one Tag, and there is/are Row objects that have more than one Tag among the one filtered by, Sum('amount') counts it/them once per Tag, even if I use distinct(). Please note also that I assert, in the test, that the filtered queryset is composed by three Row objects, as expected, but the next assert fails, with 40 != 30. I added a test that instead of aggregate(Sum('amount')) does sum([x.amount for x in rows]) which passes. Is this a bug in Sum() or am I missing something? Kind regards, Mark -- -- Ticket URL: <https://code.djangoproject.com/ticket/24385#comment:1> Django <https://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-updates+unsubscr...@googlegroups.com. To post to this group, send email to django-updates@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/063.fc76d942a3ab2e93e7dd3b750b97e735%40djangoproject.com. For more options, visit https://groups.google.com/d/optout.