Skip to content

Commit a694dbb

Browse files
committed
Closes #5990: Deprecated display_field parameter for custom script ObjectVar and MultiObjectVar fields
1 parent f74c880 commit a694dbb

File tree

4 files changed

+17
-12
lines changed

4 files changed

+17
-12
lines changed

docs/additional-features/custom-scripts.md

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -170,18 +170,13 @@ Similar to `ChoiceVar`, but allows for the selection of multiple choices.
170170
A particular object within NetBox. Each ObjectVar must specify a particular model, and allows the user to select one of the available instances. ObjectVar accepts several arguments, listed below.
171171

172172
* `model` - The model class
173-
* `display_field` - The name of the REST API object field to display in the selection list (default: `'name'`)
173+
* `display_field` - The name of the REST API object field to display in the selection list (default: `'display'`)
174174
* `query_params` - A dictionary of query parameters to use when retrieving available options (optional)
175175
* `null_option` - A label representing a "null" or empty choice (optional)
176176

177-
The `display_field` argument is useful when referencing a model which does not have a `name` field. For example, when displaying a list of device types, you would likely use the `model` field:
178-
179-
```python
180-
device_type = ObjectVar(
181-
model=DeviceType,
182-
display_field='model'
183-
)
184-
```
177+
!!! warning
178+
The `display_field` parameter is now deprecated, and will be removed in NetBox v2.12. All ObjectVar instances will
179+
instead use the new standard `display` field for all serializers (introduced in NetBox v2.11).
185180

186181
To limit the selections available within the list, additional query parameters can be passed as the `query_params` dictionary. For example, to show only devices with an "active" status:
187182

docs/release-notes/version-2.11.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ The ObjectChange model (which is used to record the creation, modification, and
8484

8585
* [#1638](https://github.com/netbox-community/netbox/issues/1638) - Migrate all primary keys to 64-bit integers
8686
* [#5873](https://github.com/netbox-community/netbox/issues/5873) - Use numeric IDs in all object URLs
87+
* [#5990](https://github.com/netbox-community/netbox/issues/5990) - Deprecated `display_field` parameter for custom script ObjectVar and MultiObjectVar fields
8788

8889
### REST API Changes
8990

netbox/extras/scripts.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,14 +180,22 @@ class ObjectVar(ScriptVariable):
180180
A single object within NetBox.
181181
182182
:param model: The NetBox model being referenced
183-
:param display_field: The attribute of the returned object to display in the selection list (default: 'name')
183+
:param display_field: The attribute of the returned object to display in the selection list (DEPRECATED)
184184
:param query_params: A dictionary of additional query parameters to attach when making REST API requests (optional)
185185
:param null_option: The label to use as a "null" selection option (optional)
186186
"""
187187
form_field = DynamicModelChoiceField
188188

189-
def __init__(self, model=None, queryset=None, display_field='name', query_params=None, null_option=None, *args,
190-
**kwargs):
189+
def __init__(self, model=None, queryset=None, query_params=None, null_option=None, *args, **kwargs):
190+
191+
# TODO: Remove display_field in v2.12
192+
if 'display_field' in kwargs:
193+
warnings.warn(
194+
"The 'display_field' parameter has been deprecated, and will be removed in NetBox v2.12. Object "
195+
"variables will now reference the 'display' attribute available on all model serializers by default."
196+
)
197+
display_field = kwargs.pop('display_field', 'display')
198+
191199
super().__init__(*args, **kwargs)
192200

193201
# Set the form field's queryset. Support backward compatibility for the "queryset" argument for now.

netbox/utilities/forms/fields.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ class DynamicModelChoiceMixin:
276276
filter = django_filters.ModelChoiceFilter
277277
widget = widgets.APISelect
278278

279+
# TODO: Remove display_field in v2.12
279280
def __init__(self, display_field='display', query_params=None, initial_params=None, null_option=None,
280281
disabled_indicator=None, brief_mode=True, *args, **kwargs):
281282
self.display_field = display_field

0 commit comments

Comments
 (0)