mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-14 09:51:22 -06:00
* 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
This commit is contained in:
parent
685264c757
commit
0cda10a204
@ -1,6 +1,8 @@
|
|||||||
from django.apps import apps
|
from django.apps import apps
|
||||||
from django.contrib.contenttypes.fields import GenericForeignKey
|
from django.contrib.contenttypes.fields import GenericForeignKey
|
||||||
|
from django.core.exceptions import ValidationError
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
from django.utils.translation import gettext_lazy as _
|
||||||
from dcim.constants import LOCATION_SCOPE_TYPES
|
from dcim.constants import LOCATION_SCOPE_TYPES
|
||||||
|
|
||||||
__all__ = (
|
__all__ = (
|
||||||
@ -84,6 +86,16 @@ class CachedScopeMixin(models.Model):
|
|||||||
class Meta:
|
class Meta:
|
||||||
abstract = True
|
abstract = True
|
||||||
|
|
||||||
|
def clean(self):
|
||||||
|
if self.scope_type and not self.scope:
|
||||||
|
scope_type = self.scope_type.model_class()
|
||||||
|
raise ValidationError({
|
||||||
|
'scope': _(
|
||||||
|
"Please select a {scope_type}."
|
||||||
|
).format(scope_type=scope_type._meta.model_name)
|
||||||
|
})
|
||||||
|
super().clean()
|
||||||
|
|
||||||
def save(self, *args, **kwargs):
|
def save(self, *args, **kwargs):
|
||||||
# Cache objects associated with the terminating object (for filtering)
|
# Cache objects associated with the terminating object (for filtering)
|
||||||
self.cache_related_objects()
|
self.cache_related_objects()
|
||||||
|
Loading…
Reference in New Issue
Block a user