10408 add error message if already exists

This commit is contained in:
Arthur 2022-09-26 13:50:44 -07:00
parent 96784640e3
commit b833831bda

View File

@ -1,5 +1,6 @@
from django.contrib.contenttypes.fields import GenericForeignKey from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import ValidationError
from django.db import models from django.db import models
from django.urls import reverse from django.urls import reverse
from mptt.models import TreeForeignKey from mptt.models import TreeForeignKey
@ -119,7 +120,7 @@ class Contact(NetBoxModel):
class Meta: class Meta:
ordering = ['name'] ordering = ['name']
unique_together = ( unique_together = (
('group', 'name') ('group', 'name'),
) )
def __str__(self): def __str__(self):
@ -168,3 +169,11 @@ class ContactAssignment(WebhooksMixin, ChangeLoggedModel):
def get_absolute_url(self): def get_absolute_url(self):
return reverse('tenancy:contact', args=[self.contact.pk]) 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)