Created
September 26, 2017 16:14
-
-
Save martinsam/bdadc856c869da763d52281766d9fb4b to your computer and use it in GitHub Desktop.
Delete duplicate entry - Django
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.