Changes between Initial Version and Version 1 of Ticket #32663


Ignore:
Timestamp:
Apr 18, 2021, 12:51:14 PM (4 years ago)
Author:
Yovel Cohen
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #32663 – Description

    initial v1  
    33
    44the error being raised is:
    5 ```
     5
     6{{{
    67NotImplementedError: annotate() + distinct(fields) is not implemented.
    7 ```
     8
     9}}}
     10
    811after looking at the source code for the exception at django.db.models.sql.compiler.SQLCompiler
    912row 594
    10 ```
     13
     14{{{
    1115if grouping:
    1216        if distinct_fields:
     
    1519        result.append('GROUP BY %s' % ', '.join(grouping))
    1620# rest of the as_sql() method
    17 ```
     21
     22}}}
    1823
    1924after just removing the distinct_fields condition:
    20 ```
     25
     26
     27{{{
    2128if grouping:
    2229        order_by = order_by or self.connection.ops.force_no_ordering()
    2330        result.append('GROUP BY %s' % ', '.join(grouping))
    2431# rest of the as_sql() method
    25 ```
     32}}}
     33
     34
    2635
    2736it works, at least in the following ways I tried (annotations are just made up for sake of the example)
    28 ```
     37
     38{{{
     39
    2940model_scores_latest_date_annotation = Max('model_scores__date')
    3041latest_score_annotation = Case(When(model_scores__date=F('latest_date'), then='model_scores__score')
    3142base_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
     46all the following distinct calls worked:
     47
     48{{{
     49
    3550query_set = base_query_set.order_by('latest_date').distinct('id', 'latest_date')
    3651
     
    3853
    3954query_set = base_query_set.distinct('id', 'latest_date')
    40 ```
     55
     56}}}
     57
    4158
    4259which makes me think that the as_sql method on SQLCompiler can handle more cases easily and this just fell beneath the cracks.
Back to Top