From fe1ffdcbb7938623791e1aeb0dd81eca670f7f79 Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Wed, 12 Aug 2020 14:51:58 -0400 Subject: [PATCH] Extend ObjectVars to accept display_field, query_params, and null_option --- netbox/extras/scripts.py | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/netbox/extras/scripts.py b/netbox/extras/scripts.py index c3957d4be..b296e140c 100644 --- a/netbox/extras/scripts.py +++ b/netbox/extras/scripts.py @@ -170,28 +170,42 @@ class ChoiceVar(ScriptVariable): class ObjectVar(ScriptVariable): """ A single object within NetBox. + + :param model: The NetBox model being referenced + :param display_field: The attribute of the returned object to display in the selection list (default: 'name') + :param query_params: A dictionary of additional query parameters to attach when making REST API requests (optional) + :param null_option: The label to use as a "null" selection option (optional) """ form_field = DynamicModelChoiceField - def __init__(self, queryset, *args, **kwargs): + def __init__(self, model=None, queryset=None, display_field='name', query_params=None, null_option=None, *args, + **kwargs): super().__init__(*args, **kwargs) - # Queryset for field choices - self.field_attrs['queryset'] = queryset + # Set the form field's queryset. Support backward compatibility for the "queryset" argument for now. + if model is not None: + self.field_attrs['queryset'] = model.objects.all() + elif queryset is not None: + warnings.warn( + f'{self}: Specifying a queryset for ObjectVar is no longer supported. Please use "model" instead.' + ) + self.field_attrs['queryset'] = queryset + else: + raise TypeError('ObjectVar must specify a model') + + self.field_attrs.update({ + 'display_field': display_field, + 'query_params': query_params, + 'null_option': null_option, + }) -class MultiObjectVar(ScriptVariable): +class MultiObjectVar(ObjectVar): """ Like ObjectVar, but can represent one or more objects. """ form_field = DynamicModelMultipleChoiceField - def __init__(self, queryset, *args, **kwargs): - super().__init__(*args, **kwargs) - - # Queryset for field choices - self.field_attrs['queryset'] = queryset - class FileVar(ScriptVariable): """