mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-22 03:56:53 -06:00
Fixes GenericForeignKey validation (#11550)
* added model validation for GenericForeignKey * added ct_field and fk_field null validation * applied suggestion
This commit is contained in:
parent
2c07762b7a
commit
b41f8755df
@ -1,4 +1,5 @@
|
|||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
from django.contrib.contenttypes.fields import GenericForeignKey
|
||||||
from django.core.validators import ValidationError
|
from django.core.validators import ValidationError
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from mptt.models import MPTTModel, TreeForeignKey
|
from mptt.models import MPTTModel, TreeForeignKey
|
||||||
@ -58,6 +59,33 @@ class NetBoxModel(CloningMixin, NetBoxFeatureSet, models.Model):
|
|||||||
class Meta:
|
class Meta:
|
||||||
abstract = True
|
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):
|
||||||
|
ct_field = getattr(self, field.ct_field)
|
||||||
|
fk_field = getattr(self, field.fk_field)
|
||||||
|
|
||||||
|
if ct_field is None and fk_field is not None:
|
||||||
|
raise ValidationError({
|
||||||
|
field.ct_field: "This field cannot be null.",
|
||||||
|
})
|
||||||
|
if fk_field is None and ct_field is not None:
|
||||||
|
raise ValidationError({
|
||||||
|
field.fk_field: "This field cannot be null.",
|
||||||
|
})
|
||||||
|
|
||||||
|
if ct_field and fk_field:
|
||||||
|
klass = getattr(self, field.ct_field).model_class()
|
||||||
|
if not klass.objects.filter(pk=fk_field).exists():
|
||||||
|
raise ValidationError({
|
||||||
|
field.fk_field: f"Invalid {fk_field}: object does not exist on {ct_field}."
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
class PrimaryModel(NetBoxModel):
|
class PrimaryModel(NetBoxModel):
|
||||||
"""
|
"""
|
||||||
|
Loading…
Reference in New Issue
Block a user