Skip to content

Instantly share code, notes, and snippets.

@melizeche
Forked from victorono/remove_duplicates.py
Last active April 19, 2020 12:55
Show Gist options
  • Save melizeche/0fef1de23cde67a23c49ee7e4633f8eb to your computer and use it in GitHub Desktop.
Save melizeche/0fef1de23cde67a23c49ee7e4633f8eb to your computer and use it in GitHub Desktop.
Django - remove duplicate objects where there is more than one field to compare
from django.db.models import Count, Max
from core.models import HelpRequest
unique_fields = ['phone', 'title']
actives = HelpRequest.objects.filter(active=True)
duplicates = (
actives.values(*unique_fields)
.order_by()
.annotate(max_id=Max('id'), count_id=Count('id'))
.filter(count_id__gt=1)
)
for duplicate in duplicates:
(
HelpRequest.objects
.filter(**{x: duplicate[x] for x in unique_fields})
.exclude(id=duplicate['max_id'])
.update(active=False)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment