mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-22 12:06:53 -06:00
12328 update GFK object in clean (#13946)
* 12328 update GFK object in clean * Add missing import statement --------- Co-authored-by: Jeremy Stretch <jstretch@netboxlabs.com>
This commit is contained in:
parent
6dc560596d
commit
6093debb71
@ -1,5 +1,6 @@
|
||||
from django.conf import settings
|
||||
from django.contrib.contenttypes.fields import GenericForeignKey
|
||||
from django.core.exceptions import ObjectDoesNotExist
|
||||
from django.core.validators import ValidationError
|
||||
from django.db import models
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
@ -85,11 +86,16 @@ class NetBoxModel(NetBoxFeatureSet, models.Model):
|
||||
|
||||
if ct_value and fk_value:
|
||||
klass = getattr(self, field.ct_field).model_class()
|
||||
if not klass.objects.filter(pk=fk_value).exists():
|
||||
try:
|
||||
obj = klass.objects.get(pk=fk_value)
|
||||
except ObjectDoesNotExist:
|
||||
raise ValidationError({
|
||||
field.fk_field: f"Related object not found using the provided value: {fk_value}."
|
||||
})
|
||||
|
||||
# update the GFK field value
|
||||
setattr(self, field.name, obj)
|
||||
|
||||
|
||||
#
|
||||
# NetBox internal base models
|
||||
|
Loading…
Reference in New Issue
Block a user