mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
Fetch choices during form initialization
This commit is contained in:
@ -128,10 +128,7 @@ class ProviderFilterForm(BootstrapMixin, CustomFieldFilterForm):
|
|||||||
required=False,
|
required=False,
|
||||||
label='ASN'
|
label='ASN'
|
||||||
)
|
)
|
||||||
|
tag = TagFilterField(model)
|
||||||
def __init__(self, *args, **kwargs):
|
|
||||||
super().__init__(*args, **kwargs)
|
|
||||||
self.fields['tag'] = TagFilterField(self.model)
|
|
||||||
|
|
||||||
|
|
||||||
#
|
#
|
||||||
@ -336,10 +333,7 @@ class CircuitFilterForm(BootstrapMixin, TenancyFilterForm, CustomFieldFilterForm
|
|||||||
min_value=0,
|
min_value=0,
|
||||||
label='Commit rate (Kbps)'
|
label='Commit rate (Kbps)'
|
||||||
)
|
)
|
||||||
|
tag = TagFilterField(model)
|
||||||
def __init__(self, *args, **kwargs):
|
|
||||||
super().__init__(*args, **kwargs)
|
|
||||||
self.fields['tag'] = TagFilterField(self.model)
|
|
||||||
|
|
||||||
|
|
||||||
#
|
#
|
||||||
|
@ -335,10 +335,7 @@ class SiteFilterForm(BootstrapMixin, TenancyFilterForm, CustomFieldFilterForm):
|
|||||||
value_field="slug",
|
value_field="slug",
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
tag = TagFilterField(model)
|
||||||
def __init__(self, *args, **kwargs):
|
|
||||||
super().__init__(*args, **kwargs)
|
|
||||||
self.fields['tag'] = TagFilterField(self.model)
|
|
||||||
|
|
||||||
|
|
||||||
#
|
#
|
||||||
@ -717,10 +714,7 @@ class RackFilterForm(BootstrapMixin, TenancyFilterForm, CustomFieldFilterForm):
|
|||||||
null_option=True,
|
null_option=True,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
tag = TagFilterField(model)
|
||||||
def __init__(self, *args, **kwargs):
|
|
||||||
super().__init__(*args, **kwargs)
|
|
||||||
self.fields['tag'] = TagFilterField(self.model)
|
|
||||||
|
|
||||||
|
|
||||||
#
|
#
|
||||||
@ -1013,10 +1007,7 @@ class DeviceTypeFilterForm(BootstrapMixin, CustomFieldFilterForm):
|
|||||||
choices=BOOLEAN_WITH_BLANK_CHOICES
|
choices=BOOLEAN_WITH_BLANK_CHOICES
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
tag = TagFilterField(model)
|
||||||
def __init__(self, *args, **kwargs):
|
|
||||||
super().__init__(*args, **kwargs)
|
|
||||||
self.fields['tag'] = TagFilterField(self.model)
|
|
||||||
|
|
||||||
|
|
||||||
#
|
#
|
||||||
@ -1959,10 +1950,7 @@ class DeviceFilterForm(BootstrapMixin, LocalConfigContextFilterForm, TenancyFilt
|
|||||||
choices=BOOLEAN_WITH_BLANK_CHOICES
|
choices=BOOLEAN_WITH_BLANK_CHOICES
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
tag = TagFilterField(model)
|
||||||
def __init__(self, *args, **kwargs):
|
|
||||||
super().__init__(*args, **kwargs)
|
|
||||||
self.fields['tag'] = TagFilterField(self.model)
|
|
||||||
|
|
||||||
|
|
||||||
#
|
#
|
||||||
@ -3435,10 +3423,7 @@ class InventoryItemFilterForm(BootstrapMixin, forms.Form):
|
|||||||
choices=BOOLEAN_WITH_BLANK_CHOICES
|
choices=BOOLEAN_WITH_BLANK_CHOICES
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
tag = TagFilterField(model)
|
||||||
def __init__(self, *args, **kwargs):
|
|
||||||
super().__init__(*args, **kwargs)
|
|
||||||
self.fields['tag'] = TagFilterField(self.model)
|
|
||||||
|
|
||||||
|
|
||||||
#
|
#
|
||||||
@ -3625,10 +3610,7 @@ class VirtualChassisFilterForm(BootstrapMixin, CustomFieldFilterForm):
|
|||||||
null_option=True,
|
null_option=True,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
tag = TagFilterField(model)
|
||||||
def __init__(self, *args, **kwargs):
|
|
||||||
super().__init__(*args, **kwargs)
|
|
||||||
self.fields['tag'] = TagFilterField(self.model)
|
|
||||||
|
|
||||||
|
|
||||||
#
|
#
|
||||||
@ -4006,7 +3988,4 @@ class PowerFeedFilterForm(BootstrapMixin, CustomFieldFilterForm):
|
|||||||
max_utilization = forms.IntegerField(
|
max_utilization = forms.IntegerField(
|
||||||
required=False
|
required=False
|
||||||
)
|
)
|
||||||
|
tag = TagFilterField(model)
|
||||||
def __init__(self, *args, **kwargs):
|
|
||||||
super().__init__(*args, **kwargs)
|
|
||||||
self.fields['tag'] = TagFilterField(self.model)
|
|
||||||
|
@ -102,10 +102,7 @@ class VRFFilterForm(BootstrapMixin, TenancyFilterForm, CustomFieldFilterForm):
|
|||||||
required=False,
|
required=False,
|
||||||
label='Search'
|
label='Search'
|
||||||
)
|
)
|
||||||
|
tag = TagFilterField(model)
|
||||||
def __init__(self, *args, **kwargs):
|
|
||||||
super().__init__(*args, **kwargs)
|
|
||||||
self.fields['tag'] = TagFilterField(self.model)
|
|
||||||
|
|
||||||
|
|
||||||
#
|
#
|
||||||
@ -235,10 +232,7 @@ class AggregateFilterForm(BootstrapMixin, CustomFieldFilterForm):
|
|||||||
value_field="slug",
|
value_field="slug",
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
tag = TagFilterField(model)
|
||||||
def __init__(self, *args, **kwargs):
|
|
||||||
super().__init__(*args, **kwargs)
|
|
||||||
self.fields['tag'] = TagFilterField(self.model)
|
|
||||||
|
|
||||||
|
|
||||||
#
|
#
|
||||||
@ -585,10 +579,7 @@ class PrefixFilterForm(BootstrapMixin, TenancyFilterForm, CustomFieldFilterForm)
|
|||||||
required=False,
|
required=False,
|
||||||
label='Expand prefix hierarchy'
|
label='Expand prefix hierarchy'
|
||||||
)
|
)
|
||||||
|
tag = TagFilterField(model)
|
||||||
def __init__(self, *args, **kwargs):
|
|
||||||
super().__init__(*args, **kwargs)
|
|
||||||
self.fields['tag'] = TagFilterField(self.model)
|
|
||||||
|
|
||||||
|
|
||||||
#
|
#
|
||||||
@ -1017,10 +1008,7 @@ class IPAddressFilterForm(BootstrapMixin, TenancyFilterForm, CustomFieldFilterFo
|
|||||||
choices=BOOLEAN_WITH_BLANK_CHOICES
|
choices=BOOLEAN_WITH_BLANK_CHOICES
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
tag = TagFilterField(model)
|
||||||
def __init__(self, *args, **kwargs):
|
|
||||||
super().__init__(*args, **kwargs)
|
|
||||||
self.fields['tag'] = TagFilterField(self.model)
|
|
||||||
|
|
||||||
|
|
||||||
#
|
#
|
||||||
@ -1308,10 +1296,7 @@ class VLANFilterForm(BootstrapMixin, TenancyFilterForm, CustomFieldFilterForm):
|
|||||||
null_option=True,
|
null_option=True,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
tag = TagFilterField(model)
|
||||||
def __init__(self, *args, **kwargs):
|
|
||||||
super().__init__(*args, **kwargs)
|
|
||||||
self.fields['tag'] = TagFilterField(self.model)
|
|
||||||
|
|
||||||
|
|
||||||
#
|
#
|
||||||
@ -1372,10 +1357,7 @@ class ServiceFilterForm(BootstrapMixin, CustomFieldFilterForm):
|
|||||||
port = forms.IntegerField(
|
port = forms.IntegerField(
|
||||||
required=False,
|
required=False,
|
||||||
)
|
)
|
||||||
|
tag = TagFilterField(model)
|
||||||
def __init__(self, *args, **kwargs):
|
|
||||||
super().__init__(*args, **kwargs)
|
|
||||||
self.fields['tag'] = TagFilterField(self.model)
|
|
||||||
|
|
||||||
|
|
||||||
class ServiceBulkEditForm(BootstrapMixin, CustomFieldBulkEditForm):
|
class ServiceBulkEditForm(BootstrapMixin, CustomFieldBulkEditForm):
|
||||||
|
@ -184,10 +184,7 @@ class SecretFilterForm(BootstrapMixin, CustomFieldFilterForm):
|
|||||||
value_field="slug",
|
value_field="slug",
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
tag = TagFilterField(model)
|
||||||
def __init__(self, *args, **kwargs):
|
|
||||||
super().__init__(*args, **kwargs)
|
|
||||||
self.fields['tag'] = TagFilterField(self.model)
|
|
||||||
|
|
||||||
|
|
||||||
#
|
#
|
||||||
|
@ -113,10 +113,7 @@ class TenantFilterForm(BootstrapMixin, CustomFieldFilterForm):
|
|||||||
null_option=True,
|
null_option=True,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
tag = TagFilterField(model)
|
||||||
def __init__(self, *args, **kwargs):
|
|
||||||
super().__init__(*args, **kwargs)
|
|
||||||
self.fields['tag'] = TagFilterField(self.model)
|
|
||||||
|
|
||||||
|
|
||||||
#
|
#
|
||||||
|
@ -606,11 +606,16 @@ class TagFilterField(forms.MultipleChoiceField):
|
|||||||
widget = StaticSelect2Multiple
|
widget = StaticSelect2Multiple
|
||||||
|
|
||||||
def __init__(self, model, *args, **kwargs):
|
def __init__(self, model, *args, **kwargs):
|
||||||
|
# Only instanitate the field if the model supports tags (i.e. hide if not)
|
||||||
if hasattr(model, 'tags'):
|
if hasattr(model, 'tags'):
|
||||||
tags = model.tags.annotate(count=Count('extras_taggeditem_items')).order_by('name')
|
self.model = model
|
||||||
choices = [(str(tag.slug), '{} ({})'.format(tag.name, tag.count)) for tag in tags]
|
|
||||||
|
|
||||||
super().__init__(label='Tags', choices=choices, required=False, *args, **kwargs)
|
# Choices are fetched during form initialization
|
||||||
|
super().__init__(label='Tags', choices=self._choices, required=False, *args, **kwargs)
|
||||||
|
|
||||||
|
def _choices(self):
|
||||||
|
tags = self.model.tags.annotate(count=Count('extras_taggeditem_items')).order_by('name')
|
||||||
|
return [(str(tag.slug), '{} ({})'.format(tag.name, tag.count)) for tag in tags]
|
||||||
|
|
||||||
|
|
||||||
class FilterChoiceIterator(forms.models.ModelChoiceIterator):
|
class FilterChoiceIterator(forms.models.ModelChoiceIterator):
|
||||||
|
@ -216,10 +216,7 @@ class ClusterFilterForm(BootstrapMixin, CustomFieldFilterForm):
|
|||||||
null_option=True,
|
null_option=True,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
tag = TagFilterField(model)
|
||||||
def __init__(self, *args, **kwargs):
|
|
||||||
super().__init__(*args, **kwargs)
|
|
||||||
self.fields['tag'] = TagFilterField(self.model)
|
|
||||||
|
|
||||||
|
|
||||||
class ClusterAddDevicesForm(BootstrapMixin, ChainedFieldsMixin, forms.Form):
|
class ClusterAddDevicesForm(BootstrapMixin, ChainedFieldsMixin, forms.Form):
|
||||||
@ -626,10 +623,7 @@ class VirtualMachineFilterForm(BootstrapMixin, TenancyFilterForm, CustomFieldFil
|
|||||||
required=False,
|
required=False,
|
||||||
label='MAC address'
|
label='MAC address'
|
||||||
)
|
)
|
||||||
|
tag = TagFilterField(model)
|
||||||
def __init__(self, *args, **kwargs):
|
|
||||||
super().__init__(*args, **kwargs)
|
|
||||||
self.fields['tag'] = TagFilterField(self.model)
|
|
||||||
|
|
||||||
|
|
||||||
#
|
#
|
||||||
|
Reference in New Issue
Block a user