Skip to content

Instantly share code, notes, and snippets.

@Bastien-Brd
Created October 14, 2016 09:41
Show Gist options
  • Save Bastien-Brd/2ce507b3427bbd65d42030830315cb98 to your computer and use it in GitHub Desktop.
Save Bastien-Brd/2ce507b3427bbd65d42030830315cb98 to your computer and use it in GitHub Desktop.
Django - ORM - Find duplicate objects based on a specific field
"""
Suppose you have a django model called MyModel with a field called 'name'.
If for some reason you did not set a "unique" constraint on that field but would still like to avoid duplicate objects with teh same name,
here is how to find all the duplicate objects based on that "name" field.
"""
duplicate_names = MyModel.objects.values('name', 'id').annotate(Count('name')).order_by().filter(name__count__gt=1)
# You can then retrieve all these duplicate objects using this query:
duplicate_objects = MyModel.objects.filter(id__in=[item['id'] for item in duplicate_names])
@UpenderSingh
Copy link

ids = MyModel.objects.values('name', 'id').annotate(Count('name')).order_by().filter(name__count__gt=1).values_list("id", flat=True)

duplicate_objects = MyModel.objects.filter(id__in=ids)

@jjconti
Copy link

jjconti commented Aug 28, 2018

from django.db.models import Count

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment