diff --git a/api/customs/management/commands/eliminar_pedimentos_duplicados.py b/api/customs/management/commands/eliminar_pedimentos_duplicados.py index 760755b..ef6897c 100644 --- a/api/customs/management/commands/eliminar_pedimentos_duplicados.py +++ b/api/customs/management/commands/eliminar_pedimentos_duplicados.py @@ -9,14 +9,14 @@ class Command(BaseCommand): def handle(self, *args, **options): duplicados = ( Pedimento.objects - .values('pedimento_app') + .values('pedimento') .annotate(total=Count('id')) .filter(total__gt=1) ) for dup in duplicados: pedimentos = ( Pedimento.objects - .filter(pedimento_app=dup['pedimento_app']) + .filter(pedimento=dup['pedimento']) .annotate( num_docs=Count('documents'), num_coves=Count('coves'), @@ -28,10 +28,10 @@ class Command(BaseCommand): # Mantener el primero, eliminar los demás to_delete = pedimentos[1:] for ped in to_delete: - self.stdout.write(f'Eliminando pedimento {ped.id} ({ped.pedimento_app})') + self.stdout.write(f'Eliminando pedimento {ped.id} ({ped.pedimento})') Cove.objects.filter(pedimento=ped).delete() EDocument.objects.filter(pedimento=ped).delete() Partida.objects.filter(pedimento=ped).delete() Document.objects.filter(pedimento=ped).delete() ped.delete() - self.stdout.write(self.style.SUCCESS('Eliminación de duplicados completada.')) + self.stdout.write(self.style.SUCCESS('Eliminación de duplicados completada.')) \ No newline at end of file diff --git a/api/customs/migrations/0016_alter_pedimento_unique_together.py b/api/customs/migrations/0016_alter_pedimento_unique_together.py new file mode 100644 index 0000000..8cbbd27 --- /dev/null +++ b/api/customs/migrations/0016_alter_pedimento_unique_together.py @@ -0,0 +1,18 @@ +# Generated by Django 5.2.3 on 2025-10-07 00:36 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('customs', '0015_partida_updated_at'), + ('organization', '0002_remove_organizacion_membretado_and_more'), + ] + + operations = [ + migrations.AlterUniqueTogether( + name='pedimento', + unique_together={('organizacion', 'pedimento'), ('organizacion', 'pedimento_app')}, + ), + ] diff --git a/api/customs/models.py b/api/customs/models.py index 01e993c..84fccba 100644 --- a/api/customs/models.py +++ b/api/customs/models.py @@ -60,6 +60,10 @@ class Pedimento(models.Model): verbose_name_plural = "Pedimentos" db_table = 'pedimento' ordering = ['pedimento'] + unique_together = [ + ['organizacion', 'pedimento'], + ['organizacion', 'pedimento_app'] + ] class Partida(models.Model): pedimento = models.ForeignKey(Pedimento, on_delete=models.CASCADE, related_name='partidas', help_text="Pedimento asociado a la partida")