1
0
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:
Jeremy Stretch
2020-08-12 09:47:13 -04:00
parent f3fb85933f
commit 8a8b4e728a
7 changed files with 42 additions and 86 deletions

View File

@ -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)