This repository has been archived by the owner on May 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: new management to remove duplicate records
- Loading branch information
Showing
2 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
"""The following removes duplicate Objects by their item_id field. | ||
This has not been strongly tested, so please be careful when running this command. | ||
""" | ||
|
||
|
||
from django.core.management.base import BaseCommand | ||
from django.db.models import Count | ||
|
||
from postcards.models import Object | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "Remove duplicate Objects by their item_id field." | ||
|
||
def handle(self, *args, **options): | ||
duplicates = ( | ||
Object.objects.values("item_id") | ||
.annotate(item_count=Count("item_id")) | ||
.filter(item_count__gt=1) | ||
) | ||
for duplicate in duplicates: | ||
item_id = duplicate["item_id"] | ||
Object.objects.filter(item_id=item_id).delete() | ||
self.stdout.write( | ||
self.style.SUCCESS( | ||
f"Successfully removed duplicate Objects with item_id: {item_id}" | ||
) | ||
) | ||
self.stdout.write( | ||
self.style.SUCCESS("Successfully removed all duplicate Objects") | ||
) |
25 changes: 25 additions & 0 deletions
25
postcards/migrations/0070_alter_transcription_postal_object.py
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Generated by Django 4.2.11 on 2024-04-26 17:42 | ||
|
||
import django.db.models.deletion | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("postcards", "0069_remove_primarysource_file"), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name="transcription", | ||
name="postal_object", | ||
field=models.ForeignKey( | ||
blank=True, | ||
help_text="Link a document to the transcription.", | ||
null=True, | ||
on_delete=django.db.models.deletion.SET_NULL, | ||
related_name="transcriptions", | ||
to="postcards.object", | ||
), | ||
), | ||
] |