25
In order to count the number of occurrences of each type, you have to group by the type
field. In Django this is done by using values
to get just that field. So, this should work:
Item.objects.values('group').annotate(
type_count=models.Count("type")
).filter(type_count__gt=1).order_by("-type_count")
3
It’s logical error
type_count__gt=1
means type_count > 1
so if the count == 1
it won’t be displayed
use type_count__gte=1
instead – it means type_count >= 1
- Is it possible to force queryset evaluation while keeping it a queryset
- Reordering fields in Django model
- Filter queryset by reverse exists check in Django
- LDAP authentication with django REST
Source:stackexchange.com