[Answered ]-Get annual count within the given range of years

2👍

✅

Probably you have ordering field in Meta class for Animal model, so additional field added to group by and generate wrong result. You can try to remove ordering, you can read about this in docs:

    Animal.objects.filter(
     date_of_birth__year__range = (
        '2017', '2018'   
     )
).extra(
    select={'year': 'YEAR(date_of_birth)'}
).values('year').annotate(total=Count('id')).order_by()

Also second values not needed.

Leave a comment