1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

Add disabled_indicator to DynamicModelChoiceMixin

This commit is contained in:
Jeremy Stretch
2020-08-12 13:30:49 -04:00
parent 779686425a
commit e9e77fc689
2 changed files with 27 additions and 37 deletions

View File

@ -249,14 +249,18 @@ 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 null_option: The string used to represent a null selection (if any)
:param disabled_indicator: The name of the field which, if populated, will disable selection of the
choice (optional)
"""
filter = django_filters.ModelChoiceFilter
widget = widgets.APISelect
def __init__(self, *args, display_field='name', query_params=None, null_option=None, **kwargs):
def __init__(self, *args, display_field='name', query_params=None, null_option=None, disabled_indicator=None,
**kwargs):
self.display_field = display_field
self.query_params = query_params or {}
self.null_option = null_option
self.disabled_indicator = disabled_indicator
# to_field_name is set by ModelChoiceField.__init__(), but we need to set it early for reference
# by widget_attrs()
@ -277,6 +281,10 @@ class DynamicModelChoiceMixin:
if self.null_option is not None:
attrs['data-null-option'] = self.null_option
# Set the disabled indicator, if any
if self.disabled_indicator is not None:
attrs['disabled-indicator'] = self.disabled_indicator
# Attach any static query parameters
for key, value in self.query_params.items():
widget.add_additional_query_param(key, value)