I am trying to get a distinct list of items. The db has a created field which is datetime and I need it as a date for my query. So I added an annotation. The problem is that distinct won't work on the annotation...
distinct_failed_recharges = recharges.filter(
status=FAILED
).annotate(
created_date=TruncDate('created')
).distinct(
'created_date', 'sim', 'product_type', 'failure_reason'
).values_list('id', flat=True)
This is the error that I get:
django.core.exceptions.FieldError: Cannot resolve keyword 'created_date' into field
I get the same error in django 1.11 doing:
qs = queryset.annotate(day=TruncDay('date')).distinct('day')
ids = list(qs.values_list('id', flat=True))
results with this error:
FieldError: Cannot resolve keyword 'day' into field.
This is very weird since I try to evaluate 'id'...
The only workaround that I've found is:
qs = queryset.annotate(day=TruncDay('date')).distinct('day')
objects_list = list(qs)
ids = [object.id for object in objects_list]
This is very inefficient, but hopefully my list is not too long...
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