Skip to content

Commit 0cda10a

Browse files
authored
Fixes: #18203 - Validate that scope is selected if scope type is specified (#18254)
* Validate that a scope has been selected if a scope_type is specified, on CachedScopeMixin models * Cleaner logic * Call super().clean() after validating scope_type/scope
1 parent 685264c commit 0cda10a

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

netbox/dcim/models/mixins.py

+12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from django.apps import apps
22
from django.contrib.contenttypes.fields import GenericForeignKey
3+
from django.core.exceptions import ValidationError
34
from django.db import models
5+
from django.utils.translation import gettext_lazy as _
46
from dcim.constants import LOCATION_SCOPE_TYPES
57

68
__all__ = (
@@ -84,6 +86,16 @@ class CachedScopeMixin(models.Model):
8486
class Meta:
8587
abstract = True
8688

89+
def clean(self):
90+
if self.scope_type and not self.scope:
91+
scope_type = self.scope_type.model_class()
92+
raise ValidationError({
93+
'scope': _(
94+
"Please select a {scope_type}."
95+
).format(scope_type=scope_type._meta.model_name)
96+
})
97+
super().clean()
98+
8799
def save(self, *args, **kwargs):
88100
# Cache objects associated with the terminating object (for filtering)
89101
self.cache_related_objects()

0 commit comments

Comments
 (0)