Skip to content

Commit

Permalink
Validate that a scope has been selected if a scope_type is specified,…
Browse files Browse the repository at this point in the history
… on CachedScopeMixin models
  • Loading branch information
bctiemann committed Dec 17, 2024
1 parent 13c26cc commit bf82101
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions netbox/dcim/models/mixins.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from django.apps import apps
from django.contrib.contenttypes.fields import GenericForeignKey
from django.core.exceptions import ValidationError
from django.db import models
from django.utils.translation import gettext_lazy as _
from dcim.constants import LOCATION_SCOPE_TYPES

__all__ = (
Expand Down Expand Up @@ -84,6 +86,16 @@ class CachedScopeMixin(models.Model):
class Meta:
abstract = True

def clean(self):
if self.scope_type:
scope_type = self.scope_type.model_class()
if not self.scope:
raise ValidationError({
'scope': _(
"Please select a {scope_type}."
).format(scope_type=scope_type._meta.model_name)
})

def save(self, *args, **kwargs):
# Cache objects associated with the terminating object (for filtering)
self.cache_related_objects()
Expand Down

0 comments on commit bf82101

Please sign in to comment.