from django.db import models
unique_fields = ['company_id', 'applicant_id']
duplicates = (CertificationApplicantCompany.objects.values(*unique_fields)
.order_by()
.annotate(max_id=models.Max('id'),
count_id=models.Count('id'))
.filter(count_id__gt=1))
for duplicate in duplicates:
(CertificationApplicantCompany.objects.filter(**{x: duplicate[x] for x in unique_fields})
.exclude(id=duplicate['max_id'])
.delete())