About Finding Duplicates By Multiple Fields

One of the ways to find duplicates of instances with multiple fields is to concatenate the fields into duplicate_id and search for the multiple instances with the same duplicate_id:

from django.db.models.functions import Concat, Cast
from django.db import models

queryset = MyModel.objects.annotate(
    duplicate_id=Concat(
        Cast(models.F("content_type_id"), models.CharField()),
        models.Value("|"),
        Cast(models.F("object_id"), models.CharField()),
        output_field=models.CharField(),
    )
)

for duplicate_id in (
    queryset.values_list("duplicate_id", flat=True)
    .annotate(duplicate_count=models.Count("duplicate_id"))
    .filter(duplicate_count__gt=1)
):
    the_first = None
    for instance in queryset.filter(
        duplicate_id=duplicate_id,
    ).order_by("created"):
        if not the_first:
            the_first = instance
        else:
            other = instance

Tips and Tricks Programming Databases Django 4.2 Django 3.2 Django 2.2

Django/Python Consulting

If you have a specific Django challenge or integration you'd like to solve, I'd be happy to help. Book a free 30-minute call to discuss your project, see if we're a good fit, and explore the best approach for your needs. After the call, you'll receive a tailored cost estimate based on what we discuss.