mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
Add query_params attribute to DynamicModelChoiceMixin
This commit is contained in:
@ -1746,11 +1746,9 @@ class DeviceForm(BootstrapMixin, TenancyForm, CustomFieldModelForm):
|
||||
platform = DynamicModelChoiceField(
|
||||
queryset=Platform.objects.all(),
|
||||
required=False,
|
||||
widget=APISelect(
|
||||
additional_query_params={
|
||||
"manufacturer_id": "null"
|
||||
}
|
||||
)
|
||||
query_params={
|
||||
"manufacturer_id": "null"
|
||||
}
|
||||
)
|
||||
cluster_group = DynamicModelChoiceField(
|
||||
queryset=ClusterGroup.objects.all(),
|
||||
@ -2680,24 +2678,20 @@ class InterfaceForm(InterfaceCommonForm, BootstrapMixin, forms.ModelForm):
|
||||
required=False,
|
||||
label='Untagged VLAN',
|
||||
display_field='display_name',
|
||||
widget=APISelect(
|
||||
full=True,
|
||||
additional_query_params={
|
||||
'site_id': 'null',
|
||||
},
|
||||
)
|
||||
query_params={
|
||||
'site_id': 'null',
|
||||
},
|
||||
widget=APISelect(full=True)
|
||||
)
|
||||
tagged_vlans = DynamicModelMultipleChoiceField(
|
||||
queryset=VLAN.objects.all(),
|
||||
required=False,
|
||||
label='Tagged VLANs',
|
||||
display_field='display_name',
|
||||
widget=APISelectMultiple(
|
||||
full=True,
|
||||
additional_query_params={
|
||||
'site_id': 'null',
|
||||
},
|
||||
)
|
||||
query_params={
|
||||
'site_id': 'null',
|
||||
},
|
||||
widget=APISelectMultiple(full=True)
|
||||
)
|
||||
tags = DynamicModelMultipleChoiceField(
|
||||
queryset=Tag.objects.all(),
|
||||
@ -2781,23 +2775,19 @@ class InterfaceCreateForm(ComponentCreateForm, InterfaceCommonForm):
|
||||
queryset=VLAN.objects.all(),
|
||||
required=False,
|
||||
display_field='display_name',
|
||||
widget=APISelect(
|
||||
full=True,
|
||||
additional_query_params={
|
||||
'site_id': 'null',
|
||||
},
|
||||
)
|
||||
query_params={
|
||||
'site_id': 'null',
|
||||
},
|
||||
widget=APISelect(full=True)
|
||||
)
|
||||
tagged_vlans = DynamicModelMultipleChoiceField(
|
||||
queryset=VLAN.objects.all(),
|
||||
required=False,
|
||||
display_field='display_name',
|
||||
widget=APISelectMultiple(
|
||||
full=True,
|
||||
additional_query_params={
|
||||
'site_id': 'null',
|
||||
},
|
||||
)
|
||||
query_params={
|
||||
'site_id': 'null',
|
||||
},
|
||||
widget=APISelectMultiple(full=True)
|
||||
)
|
||||
field_order = (
|
||||
'device', 'name_pattern', 'label_pattern', 'type', 'enabled', 'lag', 'mtu', 'mac_address', 'description',
|
||||
@ -2850,23 +2840,19 @@ class InterfaceBulkEditForm(
|
||||
queryset=VLAN.objects.all(),
|
||||
required=False,
|
||||
display_field='display_name',
|
||||
widget=APISelect(
|
||||
full=True,
|
||||
additional_query_params={
|
||||
'site_id': 'null',
|
||||
},
|
||||
)
|
||||
query_params={
|
||||
'site_id': 'null',
|
||||
},
|
||||
widget=APISelect(full=True)
|
||||
)
|
||||
tagged_vlans = DynamicModelMultipleChoiceField(
|
||||
queryset=VLAN.objects.all(),
|
||||
required=False,
|
||||
display_field='display_name',
|
||||
widget=APISelectMultiple(
|
||||
full=True,
|
||||
additional_query_params={
|
||||
'site_id': 'null',
|
||||
},
|
||||
)
|
||||
query_params={
|
||||
'site_id': 'null',
|
||||
},
|
||||
widget=APISelectMultiple(full=True)
|
||||
)
|
||||
|
||||
class Meta:
|
||||
|
@ -248,8 +248,9 @@ class DynamicModelChoiceMixin:
|
||||
filter = django_filters.ModelChoiceFilter
|
||||
widget = widgets.APISelect
|
||||
|
||||
def __init__(self, *args, display_field='name', **kwargs):
|
||||
def __init__(self, *args, display_field='name', query_params=None, **kwargs):
|
||||
self.display_field = display_field
|
||||
self.query_params = query_params or {}
|
||||
|
||||
# to_field_name is set by ModelChoiceField.__init__(), but we need to set it early for reference
|
||||
# by widget_attrs()
|
||||
@ -261,8 +262,15 @@ class DynamicModelChoiceMixin:
|
||||
attrs = {
|
||||
'display-field': self.display_field,
|
||||
}
|
||||
|
||||
# Set value-field attribute if the field specifies to_field_name
|
||||
if self.to_field_name:
|
||||
attrs['value-field'] = self.to_field_name
|
||||
|
||||
# Attach any static query parameters
|
||||
for key, value in self.query_params.items():
|
||||
widget.add_additional_query_param(key, value)
|
||||
|
||||
return attrs
|
||||
|
||||
def get_bound_field(self, form, field_name):
|
||||
|
@ -297,11 +297,9 @@ class VirtualMachineForm(BootstrapMixin, TenancyForm, CustomFieldModelForm):
|
||||
role = DynamicModelChoiceField(
|
||||
queryset=DeviceRole.objects.all(),
|
||||
required=False,
|
||||
widget=APISelect(
|
||||
additional_query_params={
|
||||
"vm_role": "True"
|
||||
}
|
||||
)
|
||||
query_params={
|
||||
"vm_role": "True"
|
||||
}
|
||||
)
|
||||
platform = DynamicModelChoiceField(
|
||||
queryset=Platform.objects.all(),
|
||||
@ -438,11 +436,9 @@ class VirtualMachineBulkEditForm(BootstrapMixin, AddRemoveTagsForm, CustomFieldB
|
||||
vm_role=True
|
||||
),
|
||||
required=False,
|
||||
widget=APISelect(
|
||||
additional_query_params={
|
||||
"vm_role": "True"
|
||||
}
|
||||
)
|
||||
query_params={
|
||||
"vm_role": "True"
|
||||
}
|
||||
)
|
||||
tenant = DynamicModelChoiceField(
|
||||
queryset=Tenant.objects.all(),
|
||||
@ -528,12 +524,10 @@ class VirtualMachineFilterForm(BootstrapMixin, TenancyFilterForm, CustomFieldFil
|
||||
queryset=DeviceRole.objects.filter(vm_role=True),
|
||||
to_field_name='slug',
|
||||
required=False,
|
||||
widget=APISelectMultiple(
|
||||
null_option=True,
|
||||
additional_query_params={
|
||||
'vm_role': "True"
|
||||
}
|
||||
)
|
||||
query_params={
|
||||
'vm_role': "True"
|
||||
},
|
||||
widget=APISelectMultiple(null_option=True)
|
||||
)
|
||||
status = forms.MultipleChoiceField(
|
||||
choices=VirtualMachineStatusChoices,
|
||||
@ -564,23 +558,19 @@ class VMInterfaceForm(BootstrapMixin, forms.ModelForm):
|
||||
queryset=VLAN.objects.all(),
|
||||
required=False,
|
||||
display_field='display_name',
|
||||
widget=APISelect(
|
||||
full=True,
|
||||
additional_query_params={
|
||||
'site_id': 'null',
|
||||
},
|
||||
)
|
||||
query_params={
|
||||
'site_id': 'null',
|
||||
},
|
||||
widget=APISelect(full=True)
|
||||
)
|
||||
tagged_vlans = DynamicModelMultipleChoiceField(
|
||||
queryset=VLAN.objects.all(),
|
||||
required=False,
|
||||
display_field='display_name',
|
||||
widget=APISelectMultiple(
|
||||
full=True,
|
||||
additional_query_params={
|
||||
'site_id': 'null',
|
||||
},
|
||||
)
|
||||
query_params={
|
||||
'site_id': 'null',
|
||||
},
|
||||
widget=APISelectMultiple(full=True)
|
||||
)
|
||||
tags = DynamicModelMultipleChoiceField(
|
||||
queryset=Tag.objects.all(),
|
||||
@ -668,23 +658,19 @@ class VMInterfaceCreateForm(BootstrapMixin, forms.Form):
|
||||
queryset=VLAN.objects.all(),
|
||||
required=False,
|
||||
display_field='display_name',
|
||||
widget=APISelect(
|
||||
full=True,
|
||||
additional_query_params={
|
||||
'site_id': 'null',
|
||||
},
|
||||
)
|
||||
query_params={
|
||||
'site_id': 'null',
|
||||
},
|
||||
widget=APISelect(full=True)
|
||||
)
|
||||
tagged_vlans = DynamicModelMultipleChoiceField(
|
||||
queryset=VLAN.objects.all(),
|
||||
required=False,
|
||||
display_field='display_name',
|
||||
widget=APISelectMultiple(
|
||||
full=True,
|
||||
additional_query_params={
|
||||
'site_id': 'null',
|
||||
},
|
||||
)
|
||||
query_params={
|
||||
'site_id': 'null',
|
||||
},
|
||||
widget=APISelectMultiple(full=True)
|
||||
)
|
||||
tags = DynamicModelMultipleChoiceField(
|
||||
queryset=Tag.objects.all(),
|
||||
@ -762,23 +748,19 @@ class VMInterfaceBulkEditForm(BootstrapMixin, BulkEditForm):
|
||||
queryset=VLAN.objects.all(),
|
||||
required=False,
|
||||
display_field='display_name',
|
||||
widget=APISelect(
|
||||
full=True,
|
||||
additional_query_params={
|
||||
'site_id': 'null',
|
||||
},
|
||||
)
|
||||
query_params={
|
||||
'site_id': 'null',
|
||||
},
|
||||
widget=APISelect(full=True)
|
||||
)
|
||||
tagged_vlans = DynamicModelMultipleChoiceField(
|
||||
queryset=VLAN.objects.all(),
|
||||
required=False,
|
||||
display_field='display_name',
|
||||
widget=APISelectMultiple(
|
||||
full=True,
|
||||
additional_query_params={
|
||||
'site_id': 'null',
|
||||
},
|
||||
)
|
||||
query_params={
|
||||
'site_id': 'null',
|
||||
},
|
||||
widget=APISelectMultiple(full=True)
|
||||
)
|
||||
|
||||
class Meta:
|
||||
|
Reference in New Issue
Block a user