diff --git a/netbox/utilities/forms/fields/dynamic.py b/netbox/utilities/forms/fields/dynamic.py index 3eb49c1bdb..793494b4b5 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 @@ def get_bound_field(self, form, field_name): } 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 f6db6f65c6..7e91229221 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