Django - Sum aggregates in postgreSQL do not use DISTINCT. This is mistake?

This is mistake? I am not good at SQL, but it looks like the aggregate function does not take into account the highlighted () passed to the query set.

distinct_queryset = MyModel.objects.filter(reverse_relationship__icontains='foo').distinct()

iterated_total = 0
for item in distinct_queryset:
    iterated_total += item.total

aggregate = queryset.aggregate(Sum('total'))

aggregate is Decimal('42201.20')

iterated_total is Decimal('38212.20')

aggregate is not total

+3
source share
1 answer

Update:

I finally tested it with debug = true and connections.queries.

on somequery.distinct()I get:
'sql': 'SELECT DISTINCT ............ WHERE....LIKE FOO

by the aggregate of various requests that I receive:
'sql': 'SELECT .......... WHERE...LIKE FOO

After google search, I found http://groups.google.com/group/django-users/browse_thread/thread/87cc286019c7d57c

I succeeded this way:

subquery = Father.objects.filter (sons_in = [adam, Bernard]) Father.objects.filter (pk_in = subquery) .aggregate (Sum ('age'))

. ? (, ?)

python .

+1

Source: https://habr.com/ru/post/1772979/


All Articles