Skip to content

Instantly share code, notes, and snippets.

View MarvinKweyu's full-sized avatar
👣
I am Marvin

Marvin Kweyu MarvinKweyu

👣
I am Marvin
View GitHub Profile
@MarvinKweyu
MarvinKweyu / remove_duplicates.py
Created December 1, 2023 06:42 — forked from victorono/remove_duplicates.py
Django - remove duplicate objects where there is more than one field to compare
from django.db.models import Count, Max
unique_fields = ['field_1', 'field_2']
duplicates = (
MyModel.objects.values(*unique_fields)
.order_by()
.annotate(max_id=Max('id'), count_id=Count('id'))
.filter(count_id__gt=1)
)