Skip to content
This repository has been archived by the owner on May 20, 2024. It is now read-only.

Commit

Permalink
feat: new management to remove duplicate records
Browse files Browse the repository at this point in the history
  • Loading branch information
hepplerj committed Apr 26, 2024
1 parent 1e3ce89 commit 7ee032f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
31 changes: 31 additions & 0 deletions postcards/management/commands/remove_duplicates.py
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 postcards/migrations/0070_alter_transcription_postal_object.py
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",
),
),
]

0 comments on commit 7ee032f

Please sign in to comment.