Here's my database query:
results = Attachments.objects.filter(currency='current').annotate(num_attachments=Count('article_id')).order_by("num_attachments").distinct('article_id')
The query broken down as follows (as I understand it):
I am running this on PostgreSQL, so according to the Django docs, I'm fine to run distinct() based on a field.
There is no error when I execute the query, but when I try to iterate or even print the results the following error is thrown by Django debug:
NotImplementedError at /function/
annotate() + distinct(fields) not implemented.
The more detailed traceback from the interactive prompt is:
File "<console>", line 1, in <module>
File "/Users/Pat/.virtualenvs/envsp/lib/python2.7/site-packages/django/db/models/query.py", line 118, in _result_iter
self._fill_cache()
File "/Users/Pat/.virtualenvs/envsp/lib/python2.7/site-packages/django/db/models/query.py", line 875, in _fill_cache
self._result_cache.append(self._iter.next())
File "/Users/Pat/.virtualenvs/envsp/lib/python2.7/site-packages/django/db/models/query.py", line 291, in iterator
for row in compiler.results_iter():
File "/Users/Pat/.virtualenvs/envsp/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 763, in results_iter
for rows in self.execute_sql(MULTI):
File "/Users/Pat/.virtualenvs/envsp/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 808, in execute_sql
sql, params = self.as_sql()
File "/Users/Pat/.virtualenvs/envsp/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 107, in as_sql
"annotate() + distinct(fields) not implemented.")
NotImplementedError: annotate() + distinct(fields) not implemented.
Anyone know what's going on here?
The work-around is to use values('distinct_fieldname')
because this will make the final SQL statement perform GROUP BY
on that field (you can add more than one fieldname), which essentially is the same.
For instance, if you want to know how many articles exist for a given 'filename'
you would do this:
results = Attachments.objects.filter(currency='current').values('filename').annotate(num_attachments=Count('article_id')).order_by("num_attachments")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With