Skip to content

Commit 6093deb

Browse files
12328 update GFK object in clean (#13946)
* 12328 update GFK object in clean * Add missing import statement --------- Co-authored-by: Jeremy Stretch <[email protected]>
1 parent 6dc5605 commit 6093deb

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

netbox/netbox/models/__init__.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from django.conf import settings
22
from django.contrib.contenttypes.fields import GenericForeignKey
3+
from django.core.exceptions import ObjectDoesNotExist
34
from django.core.validators import ValidationError
45
from django.db import models
56
from django.utils.translation import gettext_lazy as _
@@ -85,11 +86,16 @@ def clean(self):
8586

8687
if ct_value and fk_value:
8788
klass = getattr(self, field.ct_field).model_class()
88-
if not klass.objects.filter(pk=fk_value).exists():
89+
try:
90+
obj = klass.objects.get(pk=fk_value)
91+
except ObjectDoesNotExist:
8992
raise ValidationError({
9093
field.fk_field: f"Related object not found using the provided value: {fk_value}."
9194
})
9295

96+
# update the GFK field value
97+
setattr(self, field.name, obj)
98+
9399

94100
#
95101
# NetBox internal base models

0 commit comments

Comments
 (0)