Opened 10 years ago

Last modified 10 years ago

#24385 closed Bug

Sum() doesn't seem to respect .distinct() in the queryset filter before .aggregate() when filtering by m2m. — at Version 1

Reported by: Mark Owned by: nobody
Component: Database layer (models, ORM) Version: 1.7
Severity: Normal Keywords:
Cc: Ed Henderson Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Mark)

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

Change History (1)

comment:1 by Mark, 10 years ago

Description: modified (diff)
Summary: Problem with aggregate(Sum())Sum() doesn't seem to respect .distinct() in the queryset filter before .aggregate() when filtering by m2m.
Version: 1.61.7
Note: See TracTickets for help on using tickets.
Back to Top