Changes between Initial Version and Version 1 of Ticket #32663
- Timestamp:
- Apr 18, 2021, 12:51:14 PM (4 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
Ticket #32663 – Description
initial v1 3 3 4 4 the error being raised is: 5 ``` 5 6 {{{ 6 7 NotImplementedError: annotate() + distinct(fields) is not implemented. 7 ``` 8 9 }}} 10 8 11 after looking at the source code for the exception at django.db.models.sql.compiler.SQLCompiler 9 12 row 594 10 ``` 13 14 {{{ 11 15 if grouping: 12 16 if distinct_fields: … … 15 19 result.append('GROUP BY %s' % ', '.join(grouping)) 16 20 # rest of the as_sql() method 17 ``` 21 22 }}} 18 23 19 24 after just removing the distinct_fields condition: 20 ``` 25 26 27 {{{ 21 28 if grouping: 22 29 order_by = order_by or self.connection.ops.force_no_ordering() 23 30 result.append('GROUP BY %s' % ', '.join(grouping)) 24 31 # rest of the as_sql() method 25 ``` 32 }}} 33 34 26 35 27 36 it works, at least in the following ways I tried (annotations are just made up for sake of the example) 28 ``` 37 38 {{{ 39 29 40 model_scores_latest_date_annotation = Max('model_scores__date') 30 41 latest_score_annotation = Case(When(model_scores__date=F('latest_date'), then='model_scores__score') 31 42 base_query_set = (Model.objects.filter(**filters).alias(latest_date=model_scores_latest_date_annotation).values(ID).annotate(latest_score=latest_score_annotation, latest_date=model_scores_latest_date_annotation) 32 ``` 33 adding the following distinct calls all worked: 34 ``` 43 44 }}} 45 46 all the following distinct calls worked: 47 48 {{{ 49 35 50 query_set = base_query_set.order_by('latest_date').distinct('id', 'latest_date') 36 51 … … 38 53 39 54 query_set = base_query_set.distinct('id', 'latest_date') 40 ``` 55 56 }}} 57 41 58 42 59 which makes me think that the as_sql method on SQLCompiler can handle more cases easily and this just fell beneath the cracks.