From b833831bdaf9d2e0691a1c854a1bc1f0bf62a9dd Mon Sep 17 00:00:00 2001 From: Arthur Date: Mon, 26 Sep 2022 13:50:44 -0700 Subject: [PATCH] 10408 add error message if already exists --- netbox/tenancy/models/contacts.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/netbox/tenancy/models/contacts.py b/netbox/tenancy/models/contacts.py index 41881f853..c3c75998d 100644 --- a/netbox/tenancy/models/contacts.py +++ b/netbox/tenancy/models/contacts.py @@ -1,5 +1,6 @@ from django.contrib.contenttypes.fields import GenericForeignKey from django.contrib.contenttypes.models import ContentType +from django.core.exceptions import ValidationError from django.db import models from django.urls import reverse from mptt.models import TreeForeignKey @@ -119,7 +120,7 @@ class Contact(NetBoxModel): class Meta: ordering = ['name'] unique_together = ( - ('group', 'name') + ('group', 'name'), ) def __str__(self): @@ -168,3 +169,11 @@ class ContactAssignment(WebhooksMixin, ChangeLoggedModel): def get_absolute_url(self): return reverse('tenancy:contact', args=[self.contact.pk]) + + def validate_unique(self, exclude=None): + if ContactAssignment.objects.exclude(pk=self.pk).filter(content_type=self.content_type, object_id=self.object_id, role=self.role, priority=self.priority).exists(): + raise ValidationError({ + 'role': f'A contact assignment for this already exists.' + }) + + super().validate_unique(exclude=exclude)