Created
April 7, 2021 11:14
-
-
Save aasmpro/1e7f6e1499ed1ebe2c2b8df4d9849af8 to your computer and use it in GitHub Desktop.
find duplicated objects in django
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 characters
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