12. Find rows which have duplicate field valuesΒΆ


Say you want all users whose first_name matches another user.

You can find duplicate records using the technique below.

>>> duplicates = User.objects.values(
>>> duplicates
<QuerySet [{'first_name': 'John', 'name_count': 3}]>

If you need to fill all the records, you can do

>>> records = User.objects.filter(first_name__in=[item['first_name'] for item in duplicates])
>>> print([item.id for item in records])
[2, 11, 13]