'Using annotated value again in a Django queryset
In this annotation, I'm creating a value ever_error that is 1 if for any unique 'image__image_category__label', 'image__subject', 'image__event' combination and 0 otherwise
d = ImageAcquisitionJob.active_objects.values('image__image_category__label', 'image__subject', 'image__event')\
.annotate(
ever_error=(
Max( Case(When(has_error=True, then=Value(1)), default=Value(0), output_field=IntegerField()) )
)
)
Now, my objective is to make a new annotation that sums up the ever_error attribute for each 'image__image_category__label'
d = d.values('image__image_category__label').annotate(has_error=Sum('ever_error'))
This is not possible however because I get the error: Cannot compute Sum('ever_error'): 'ever_error' is an aggregate
The core of my problem is that I need to use the first annotation again as a regular field but this isn't permitted. Is there a way to save the annotated field so that I can use it again?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
