Last active
August 29, 2015 14:16
-
-
Save saulshanabrook/a92b27903c6a7d5076de to your computer and use it in GitHub Desktop.
Looks for all instances of a model with certain field duplicated
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
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