from festival.models import * def clean_participants(): unique_participants = [] participants = Participant.objects.all() for p in participants: if not p.name in unique_participants: unique_participants.append(p.name) for u in unique_participants: dupes = participants.filter(name=u) to_keep = dupes[0] meeting_ids = [] for d in dupes: to_keep.meetings.add(d.meeting.id) to_keep.save() for d in dupes[1:]: d.delete() print u