From aa6bb8410e4f917fcf6291ea3c613f5da06853f0 Mon Sep 17 00:00:00 2001 From: FreneticScribbler Date: Wed, 12 Jan 2022 19:15:38 +0000 Subject: [PATCH] Swap to better method of validating uniqueness of cable types SCOPE, Arona, damn it --- assets/forms.py | 8 -------- .../0022_alter_cabletype_unique_together.py | 17 +++++++++++++++++ assets/models.py | 1 + 3 files changed, 18 insertions(+), 8 deletions(-) create mode 100644 assets/migrations/0022_alter_cabletype_unique_together.py diff --git a/assets/forms.py b/assets/forms.py index 493dca1e..04469e36 100644 --- a/assets/forms.py +++ b/assets/forms.py @@ -45,11 +45,3 @@ class CableTypeForm(forms.ModelForm): class Meta: model = models.CableType fields = '__all__' - - def clean(self): # TODO Does unique_together work better than this? - form_data = self.cleaned_data - queryset = models.CableType.objects.filter(Q(plug=form_data['plug']) & Q(socket=form_data['socket']) & Q(circuits=form_data['circuits']) & Q(cores=form_data['cores'])) - # Being identical to itself shouldn't count... - if queryset.exists() and self.instance.pk != queryset[0].pk: - raise forms.ValidationError("A cable type that exactly matches this one already exists, please use that instead.", code="notunique") - return form_data diff --git a/assets/migrations/0022_alter_cabletype_unique_together.py b/assets/migrations/0022_alter_cabletype_unique_together.py new file mode 100644 index 00000000..b4b793bc --- /dev/null +++ b/assets/migrations/0022_alter_cabletype_unique_together.py @@ -0,0 +1,17 @@ +# Generated by Django 3.2.11 on 2022-01-12 19:11 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('assets', '0021_auto_20210302_1204'), + ] + + operations = [ + migrations.AlterUniqueTogether( + name='cabletype', + unique_together={('plug', 'socket', 'circuits', 'cores')}, + ), + ] diff --git a/assets/models.py b/assets/models.py index 37af4687..c46162ae 100644 --- a/assets/models.py +++ b/assets/models.py @@ -75,6 +75,7 @@ class CableType(models.Model): class Meta: ordering = ['plug', 'socket', '-circuits'] + unique_together = ['plug', 'socket', 'circuits', 'cores'] def __str__(self): if self.plug and self.socket: