From 8bf7e90bc27f264ccfb34e487f9af96a481aa1c9 Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Thu, 12 Dec 2024 12:32:26 -0500 Subject: [PATCH] Misc cleanup --- netbox/utilities/forms/fields/dynamic.py | 9 +++++++-- netbox/utilities/forms/widgets/apiselect.py | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/netbox/utilities/forms/fields/dynamic.py b/netbox/utilities/forms/fields/dynamic.py index 3eb49c1bd..793494b4b 100644 --- a/netbox/utilities/forms/fields/dynamic.py +++ b/netbox/utilities/forms/fields/dynamic.py @@ -68,6 +68,8 @@ class DynamicModelChoiceMixin: selector: Include an advanced object selection widget to assist the user in identifying the desired object quick_add: Include a widget to quickly create a new related object for assignment. NOTE: Nested usage of quick-add fields is not currently supported. + quick_add_params: A dictionary of initial data to include when launching the quick-add form (optional). The + token string "$pk" will be replaced with the primary key of the form's instance, if any. Context keys: value: The name of the attribute which contains the option's value (default: 'id') @@ -177,8 +179,11 @@ class DynamicModelChoiceMixin: } for k, v in self.quick_add_params.items(): if v == '$pk': - v = form.instance.pk - widget.quick_add_context['params'][k] = v + # Replace "$pk" token with the primary key of the form's instance (if any) + if getattr(form.instance, 'pk', None): + widget.quick_add_context['params'][k] = form.instance.pk + else: + widget.quick_add_context['params'][k] = v return bound_field diff --git a/netbox/utilities/forms/widgets/apiselect.py b/netbox/utilities/forms/widgets/apiselect.py index f6db6f65c..7e9122922 100644 --- a/netbox/utilities/forms/widgets/apiselect.py +++ b/netbox/utilities/forms/widgets/apiselect.py @@ -25,7 +25,7 @@ class APISelect(forms.Select): def get_context(self, name, value, attrs): context = super().get_context(name, value, attrs) - # Add quick-add context data, if set on the widget + # Add quick-add context data, if enabled for the widget if hasattr(self, 'quick_add_context'): context['quick_add'] = self.quick_add_context