Skip to content

Commit 52a1f9d

Browse files
committed
Add filter_fields to extras.ObjectVar & fix default value handling
1 parent e8b3d19 commit 52a1f9d

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

netbox/extras/scripts.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -181,16 +181,19 @@ class ObjectVar(ScriptVariable):
181181
182182
:param model: The NetBox model being referenced
183183
:param query_params: A dictionary of additional query parameters to attach when making REST API requests (optional)
184+
:param filter_fields: A dictionary or list of dictionaries that define a related
185+
field. Example: `{'accessor': 'group_id', 'field_name': 'tenant_group'}` (optional)
184186
:param null_option: The label to use as a "null" selection option (optional)
185187
"""
186188
form_field = DynamicModelChoiceField
187189

188-
def __init__(self, model, query_params=None, null_option=None, *args, **kwargs):
190+
def __init__(self, model, query_params=None, filter_fields=None, null_option=None, *args, **kwargs):
189191
super().__init__(*args, **kwargs)
190192

191193
self.field_attrs.update({
192194
'queryset': model.objects.all(),
193195
'query_params': query_params,
196+
'filter_fields': filter_fields,
194197
'null_option': null_option,
195198
})
196199

netbox/utilities/forms/fields.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ def widget_attrs(self, widget):
416416
widget.add_query_param(key, value)
417417

418418
# Attach any dynamic query parameters
419-
if len(self.filter_fields) > 0:
419+
if self.filter_fields is not None and len(self.filter_fields) > 0:
420420
widget.add_filter_fields(self.filter_fields)
421421

422422
return attrs

0 commit comments

Comments
 (0)