Created
October 14, 2016 09:41
-
-
Save Bastien-Brd/2ce507b3427bbd65d42030830315cb98 to your computer and use it in GitHub Desktop.
Django - ORM - Find duplicate objects based on a specific field
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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]) |
from django.db.models import Count
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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)