django queryset average of unique values
To calculate the average of unique values in a Django queryset, you can follow these steps:
- Use the
annotate
function withCount
andDistinct
to count the occurrences of each unique value in the queryset. - Use the
annotate
function again to calculate the sum of the unique values. - Use the
aggregate
function withAvg
to calculate the average of the unique values.