• ru
  • Language: en
  • Documentation version: 2.0

12. Find rows which have duplicate field values

../_images/usertable2.png

Say you want all users whose first_name matches another user.

You can find duplicate records using the technique below.

>>> duplicates = User.objects.values(
    'first_name'
    ).annotate(name_count=Count('first_name')).filter(name_count__gt=1)
>>> 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]