mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
Move null_option to DynamicModelChoiceMixin
This commit is contained in:
@@ -245,12 +245,18 @@ class TagFilterField(forms.MultipleChoiceField):
|
||||
|
||||
|
||||
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)
|
||||
"""
|
||||
filter = django_filters.ModelChoiceFilter
|
||||
widget = widgets.APISelect
|
||||
|
||||
def __init__(self, *args, display_field='name', query_params=None, **kwargs):
|
||||
def __init__(self, *args, display_field='name', query_params=None, null_option=None, **kwargs):
|
||||
self.display_field = display_field
|
||||
self.query_params = query_params or {}
|
||||
self.null_option = null_option
|
||||
|
||||
# to_field_name is set by ModelChoiceField.__init__(), but we need to set it early for reference
|
||||
# by widget_attrs()
|
||||
@@ -267,6 +273,10 @@ class DynamicModelChoiceMixin:
|
||||
if self.to_field_name:
|
||||
attrs['value-field'] = self.to_field_name
|
||||
|
||||
# Set the string used to represent a null option
|
||||
if self.null_option is not None:
|
||||
attrs['data-null-option'] = self.null_option
|
||||
|
||||
# Attach any static query parameters
|
||||
for key, value in self.query_params.items():
|
||||
widget.add_additional_query_param(key, value)
|
||||
|
@@ -146,7 +146,6 @@ class APISelect(SelectWithDisabled):
|
||||
name of the filter-for field (child field) and the value is the name of the query param filter.
|
||||
:param additional_query_params: Optional) A dict of query params to append to the API request. The key is the
|
||||
name of the query param and the value if the query param's value.
|
||||
:param null_option: If true, include the static null option in the selection list.
|
||||
"""
|
||||
def __init__(
|
||||
self,
|
||||
@@ -155,7 +154,6 @@ class APISelect(SelectWithDisabled):
|
||||
disabled_indicator=None,
|
||||
filter_for=None,
|
||||
additional_query_params=None,
|
||||
null_option=False,
|
||||
full=False,
|
||||
*args,
|
||||
**kwargs
|
||||
@@ -178,8 +176,6 @@ class APISelect(SelectWithDisabled):
|
||||
if additional_query_params:
|
||||
for key, value in additional_query_params.items():
|
||||
self.add_additional_query_param(key, value)
|
||||
if null_option:
|
||||
self.attrs['data-null-option'] = 1
|
||||
|
||||
def add_filter_for(self, name, value):
|
||||
"""
|
||||
|
Reference in New Issue
Block a user