I am working on counting rows with duplicate values in a django Model and running into a little hitch. I have looked at this question Django Select Rows with duplicate Values
and the solution is perfect. But my question is, What if I want to Count the rows and group them according to: Name of the duplicated row, and how many times it has been duplicated, instead of just displaying the name of the repeated rows.
my code so far:
views.py
dupes = Application.objects.values('school_name').annotate(Count('id')).filter(id__count__gt=1)
query = Application.objects.filter(school_name__in=[item['school_name'] for item in dupes])
The query variable returns a queryset with the Applications that have duplicate values for the field school_name while this line:
repeated_names = Application.objects.values('school_name', 'category', ).annotate(Count('id')).order_by().filter(id__count__gt=1)
returns a list of the duplicated school names.
What I expect is something like:
(school_name=school_name_1, count=2), (school_name=school_name_2, count=3).. etc.