Closes #5994: Drop support for display_field argument on ObjectVar

This commit is contained in:
jeremystretch 2021-04-21 10:29:25 -04:00
parent b509d96f18
commit 9476fda987
6 changed files with 6 additions and 29 deletions

View File

@ -170,14 +170,9 @@ Similar to `ChoiceVar`, but allows for the selection of multiple choices.
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. 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.
* `model` - The model class * `model` - The model class
* `display_field` - The name of the REST API object field to display in the selection list (default: `'display'`)
* `query_params` - A dictionary of query parameters to use when retrieving available options (optional) * `query_params` - A dictionary of query parameters to use when retrieving available options (optional)
* `null_option` - A label representing a "null" or empty choice (optional) * `null_option` - A label representing a "null" or empty choice (optional)
!!! warning
The `display_field` parameter is now deprecated, and will be removed in NetBox v2.12. All ObjectVar instances will
instead use the new standard `display` field for all serializers (introduced in NetBox v2.11).
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: 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:
```python ```python
@ -288,7 +283,6 @@ class NewBranchScript(Script):
switch_model = ObjectVar( switch_model = ObjectVar(
description="Access switch model", description="Access switch model",
model=DeviceType, model=DeviceType,
display_field='model',
query_params={ query_params={
'manufacturer_id': '$manufacturer' 'manufacturer_id': '$manufacturer'
} }

View File

@ -5,3 +5,4 @@
### Other Changes ### Other Changes
* [#5532](https://github.com/netbox-community/netbox/issues/5532) - Drop support for Python 3.6 * [#5532](https://github.com/netbox-community/netbox/issues/5532) - Drop support for Python 3.6
* [#5994](https://github.com/netbox-community/netbox/issues/5994) - Drop support for `display_field` argument on ObjectVar

View File

@ -180,27 +180,16 @@ class ObjectVar(ScriptVariable):
A single object within NetBox. A single object within NetBox.
:param model: The NetBox model being referenced :param model: The NetBox model being referenced
:param display_field: The attribute of the returned object to display in the selection list (DEPRECATED)
:param query_params: A dictionary of additional query parameters to attach when making REST API requests (optional) :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) :param null_option: The label to use as a "null" selection option (optional)
""" """
form_field = DynamicModelChoiceField form_field = DynamicModelChoiceField
def __init__(self, model, query_params=None, null_option=None, *args, **kwargs): def __init__(self, model, query_params=None, null_option=None, *args, **kwargs):
# TODO: Remove display_field in v2.12
if 'display_field' in kwargs:
warnings.warn(
"The 'display_field' parameter has been deprecated, and will be removed in NetBox v2.12. Object "
"variables will now reference the 'display' attribute available on all model serializers by default."
)
display_field = kwargs.pop('display_field', 'display')
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.field_attrs.update({ self.field_attrs.update({
'queryset': model.objects.all(), 'queryset': model.objects.all(),
'display_field': display_field,
'query_params': query_params, 'query_params': query_params,
'null_option': null_option, 'null_option': null_option,
}) })

View File

@ -201,7 +201,7 @@ $(document).ready(function() {
var results = data.results; var results = data.results;
results = results.reduce((results,record,idx) => { results = results.reduce((results,record,idx) => {
record.text = record[element.getAttribute('display-field')] || record.name; record.text = record.display;
if (record._depth) { if (record._depth) {
// Annotate hierarchical depth for MPTT objects // Annotate hierarchical depth for MPTT objects
record.text = '--'.repeat(record._depth) + ' ' + record.text; record.text = '--'.repeat(record._depth) + ' ' + record.text;

View File

@ -328,7 +328,6 @@ class ExpandableIPAddressField(forms.CharField):
class DynamicModelChoiceMixin: class DynamicModelChoiceMixin:
""" """
:param display_field: The name of the attribute of an API response object to display in the selection list
:param query_params: A dictionary of additional key/value pairs to attach to the API request :param query_params: A dictionary of additional key/value pairs to attach to the API request
:param initial_params: A dictionary of child field references to use for selecting a parent field's initial value :param initial_params: A dictionary of child field references to use for selecting a parent field's initial value
:param null_option: The string used to represent a null selection (if any) :param null_option: The string used to represent a null selection (if any)
@ -338,10 +337,8 @@ class DynamicModelChoiceMixin:
filter = django_filters.ModelChoiceFilter filter = django_filters.ModelChoiceFilter
widget = widgets.APISelect widget = widgets.APISelect
# TODO: Remove display_field in v2.12 def __init__(self, query_params=None, initial_params=None, null_option=None, disabled_indicator=None, *args,
def __init__(self, display_field='display', query_params=None, initial_params=None, null_option=None, **kwargs):
disabled_indicator=None, *args, **kwargs):
self.display_field = display_field
self.query_params = query_params or {} self.query_params = query_params or {}
self.initial_params = initial_params or {} self.initial_params = initial_params or {}
self.null_option = null_option self.null_option = null_option
@ -354,9 +351,7 @@ class DynamicModelChoiceMixin:
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
def widget_attrs(self, widget): def widget_attrs(self, widget):
attrs = { attrs = {}
'display-field': self.display_field,
}
# Set value-field attribute if the field specifies to_field_name # Set value-field attribute if the field specifies to_field_name
if self.to_field_name: if self.to_field_name:

View File

@ -667,7 +667,6 @@ class VMInterfaceCreateForm(BootstrapMixin, InterfaceCommonForm):
parent = DynamicModelChoiceField( parent = DynamicModelChoiceField(
queryset=VMInterface.objects.all(), queryset=VMInterface.objects.all(),
required=False, required=False,
display_field='display_name',
query_params={ query_params={
'virtualmachine_id': 'virtual_machine', 'virtualmachine_id': 'virtual_machine',
} }
@ -756,8 +755,7 @@ class VMInterfaceBulkEditForm(BootstrapMixin, AddRemoveTagsForm, BulkEditForm):
) )
parent = DynamicModelChoiceField( parent = DynamicModelChoiceField(
queryset=VMInterface.objects.all(), queryset=VMInterface.objects.all(),
required=False, required=False
display_field='display_name'
) )
enabled = forms.NullBooleanField( enabled = forms.NullBooleanField(
required=False, required=False,