diff --git a/assets/forms.py b/assets/forms.py index 5afc82a3..bf3e35b4 100644 --- a/assets/forms.py +++ b/assets/forms.py @@ -1,6 +1,7 @@ from django import forms from assets import models +from django.db.models import Q class AssetForm(forms.ModelForm): @@ -40,3 +41,12 @@ class CableTypeForm(forms.ModelForm): class Meta: model = models.CableType fields = '__all__' + + def clean(self): + 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 doesn't count... + print(self.instance.id) + 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/models.py b/assets/models.py index fc822137..8ec212f0 100644 --- a/assets/models.py +++ b/assets/models.py @@ -3,7 +3,6 @@ from django.core.exceptions import ValidationError from django.db import models, connection from django.urls import reverse -from django.db.models import F from django.db.models.signals import pre_save from django.dispatch.dispatcher import receiver diff --git a/assets/templates/cable_type_form.html b/assets/templates/cable_type_form.html index c8b792ad..3f44ed71 100644 --- a/assets/templates/cable_type_form.html +++ b/assets/templates/cable_type_form.html @@ -8,7 +8,11 @@ {% if create %}Create{% elif edit %}Edit{% endif %} Cable Type -