Sunday, July 21, 2019

NotImplemented error combining distinct with annotate

Hello,

Python version: Python 3.7.2
Django version: 2.2.3

I want to combine distinct and annotate(Sum(...)), and get the following error:
NotImplementedError: annotate() + distinct(fields) is not implemented.

To describe: I have a bunch of sales entries like:
'John', 100
'Sam', 50
'John', 200
(the model is a text field for salesperson and an integer field for sales_amount)

I want to write a query like:
res = Sales.objects.distinct('salesperson').annotate(Sum('sales_amount'))
for person in res:
    print('Salesperson %s sold %d dollars' % (person.salesperson, person.sales_amount__count))

I would expect:
Salesperson John sold 300 dollars
Salesperson Sam sold 50 dollars

Instead it generates the above error. Is there a way to do this?

Thank you,

John

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CABMyA2MVHvD5ARbw2UGyikkeJsgLDGbsXkBkGRdx-Bd0UbeSeA%40mail.gmail.com.

No comments:

Post a Comment