Skip to content

Instantly share code, notes, and snippets.

@saulshanabrook
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saulshanabrook/a92b27903c6a7d5076de to your computer and use it in GitHub Desktop.
Save saulshanabrook/a92b27903c6a7d5076de to your computer and use it in GitHub Desktop.
Looks for all instances of a model with certain field duplicated
def find_non_unique(model, *fields):
from collections import defaultdict
d = defaultdict(list)
for instance in model.objects.all():
d[tuple(getattr(instance, field) for field in fields)].append(instance)
return filter(lambda dups: len(dups)>1, d.values())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment