Skip to content

Instantly share code, notes, and snippets.

@aasmpro
Created April 7, 2021 11:14
Show Gist options
  • Save aasmpro/1e7f6e1499ed1ebe2c2b8df4d9849af8 to your computer and use it in GitHub Desktop.
Save aasmpro/1e7f6e1499ed1ebe2c2b8df4d9849af8 to your computer and use it in GitHub Desktop.
find duplicated objects in django
from django.db.models import Count
def get_duplicated_objects(model, field, count='id'):
dups = model.objects.values(field).annotate(Count(count)).order_by().filter(**{"{}__count__gt".format(count): 1}).values_list(field, flat=True)
return model.objects.filter(**{"{}__in".format(field): dups})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment