mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-26 09:16:10 -06:00
added model validation for GenericForeignKey
This commit is contained in:
parent
d7c37d9dd6
commit
ac81f741a0
@ -1,4 +1,5 @@
|
||||
from django.conf import settings
|
||||
from django.contrib.contenttypes.fields import GenericForeignKey
|
||||
from django.core.validators import ValidationError
|
||||
from django.db import models
|
||||
from mptt.models import MPTTModel, TreeForeignKey
|
||||
@ -58,6 +59,23 @@ class NetBoxModel(CloningMixin, NetBoxFeatureSet, models.Model):
|
||||
class Meta:
|
||||
abstract = True
|
||||
|
||||
def clean(self):
|
||||
"""
|
||||
Validate the model for GenericForeignKey fields to ensure that the content type and object ID exist.
|
||||
"""
|
||||
super().clean()
|
||||
|
||||
for field in self._meta.get_fields():
|
||||
if isinstance(field, GenericForeignKey):
|
||||
if getattr(self, field.ct_field) and getattr(self, field.fk_field):
|
||||
klass = getattr(self, field.ct_field).model_class()
|
||||
try:
|
||||
klass.objects.get(pk=getattr(self, field.fk_field))
|
||||
except klass.DoesNotExist:
|
||||
raise ValidationError({
|
||||
field.fk_field: f"Invalid {getattr(self, field.fk_field)}: object does not exist on {getattr(self, field.ct_field)}."
|
||||
})
|
||||
|
||||
|
||||
class PrimaryModel(NetBoxModel):
|
||||
"""
|
||||
|
Loading…
Reference in New Issue
Block a user