-
-
Save jleei/09c49ac46abeb30b64ef54b9b16a7e5d to your computer and use it in GitHub Desktop.
Revisions
-
victorono revised this gist
Dec 12, 2016 . 1 changed file with 3 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,9 +1,11 @@ from django.db.models import Count, Max unique_fields = ['field_1', 'field_2'] duplicates = ( MyModel.objects.values(*unique_fields) .order_by() .annotate(max_id=Max('id'), count_id=Count('id')) .filter(count_id__gt=1) ) -
victorono created this gist
Dec 12, 2016 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,16 @@ unique_fields = ['field_1', 'field_2'] duplicates = ( MyModel.objects.values(*unique_fields) .order_by() .annotate(max_id=models.Max('id'), count_id=models.Count('id')) .filter(count_id__gt=1) ) for duplicate in duplicates: ( MyModel.objects .filter(**{x: duplicate[x] for x in unique_fields}) .exclude(id=duplicate['max_id']) .delete() )