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,
|
||||
label='ASN'
|
||||
)
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.fields['tag'] = TagFilterField(self.model)
|
||||
tag = TagFilterField(model)
|
||||
|
||||
|
||||
#
|
||||
@ -336,10 +333,7 @@ class CircuitFilterForm(BootstrapMixin, TenancyFilterForm, CustomFieldFilterForm
|
||||
min_value=0,
|
||||
label='Commit rate (Kbps)'
|
||||
)
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.fields['tag'] = TagFilterField(self.model)
|
||||
tag = TagFilterField(model)
|
||||
|
||||
|
||||
#
|
||||
|
@ -335,10 +335,7 @@ class SiteFilterForm(BootstrapMixin, TenancyFilterForm, CustomFieldFilterForm):
|
||||
value_field="slug",
|
||||
)
|
||||
)
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.fields['tag'] = TagFilterField(self.model)
|
||||
tag = TagFilterField(model)
|
||||
|
||||
|
||||
#
|
||||
@ -717,10 +714,7 @@ class RackFilterForm(BootstrapMixin, TenancyFilterForm, CustomFieldFilterForm):
|
||||
null_option=True,
|
||||
)
|
||||
)
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.fields['tag'] = TagFilterField(self.model)
|
||||
tag = TagFilterField(model)
|
||||
|
||||
|
||||
#
|
||||
@ -1013,10 +1007,7 @@ class DeviceTypeFilterForm(BootstrapMixin, CustomFieldFilterForm):
|
||||
choices=BOOLEAN_WITH_BLANK_CHOICES
|
||||
)
|
||||
)
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.fields['tag'] = TagFilterField(self.model)
|
||||
tag = TagFilterField(model)
|
||||
|
||||
|
||||
#
|
||||
@ -1959,10 +1950,7 @@ class DeviceFilterForm(BootstrapMixin, LocalConfigContextFilterForm, TenancyFilt
|
||||
choices=BOOLEAN_WITH_BLANK_CHOICES
|
||||
)
|
||||
)
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.fields['tag'] = TagFilterField(self.model)
|
||||
tag = TagFilterField(model)
|
||||
|
||||
|
||||
#
|
||||
@ -3435,10 +3423,7 @@ class InventoryItemFilterForm(BootstrapMixin, forms.Form):
|
||||
choices=BOOLEAN_WITH_BLANK_CHOICES
|
||||
)
|
||||
)
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.fields['tag'] = TagFilterField(self.model)
|
||||
tag = TagFilterField(model)
|
||||
|
||||
|
||||
#
|
||||
@ -3625,10 +3610,7 @@ class VirtualChassisFilterForm(BootstrapMixin, CustomFieldFilterForm):
|
||||
null_option=True,
|
||||
)
|
||||
)
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.fields['tag'] = TagFilterField(self.model)
|
||||
tag = TagFilterField(model)
|
||||
|
||||
|
||||
#
|
||||
@ -4006,7 +3988,4 @@ class PowerFeedFilterForm(BootstrapMixin, CustomFieldFilterForm):
|
||||
max_utilization = forms.IntegerField(
|
||||
required=False
|
||||
)
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.fields['tag'] = TagFilterField(self.model)
|
||||
tag = TagFilterField(model)
|
||||
|
@ -102,10 +102,7 @@ class VRFFilterForm(BootstrapMixin, TenancyFilterForm, CustomFieldFilterForm):
|
||||
required=False,
|
||||
label='Search'
|
||||
)
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.fields['tag'] = TagFilterField(self.model)
|
||||
tag = TagFilterField(model)
|
||||
|
||||
|
||||
#
|
||||
@ -235,10 +232,7 @@ class AggregateFilterForm(BootstrapMixin, CustomFieldFilterForm):
|
||||
value_field="slug",
|
||||
)
|
||||
)
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.fields['tag'] = TagFilterField(self.model)
|
||||
tag = TagFilterField(model)
|
||||
|
||||
|
||||
#
|
||||
@ -585,10 +579,7 @@ class PrefixFilterForm(BootstrapMixin, TenancyFilterForm, CustomFieldFilterForm)
|
||||
required=False,
|
||||
label='Expand prefix hierarchy'
|
||||
)
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.fields['tag'] = TagFilterField(self.model)
|
||||
tag = TagFilterField(model)
|
||||
|
||||
|
||||
#
|
||||
@ -1017,10 +1008,7 @@ class IPAddressFilterForm(BootstrapMixin, TenancyFilterForm, CustomFieldFilterFo
|
||||
choices=BOOLEAN_WITH_BLANK_CHOICES
|
||||
)
|
||||
)
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.fields['tag'] = TagFilterField(self.model)
|
||||
tag = TagFilterField(model)
|
||||
|
||||
|
||||
#
|
||||
@ -1308,10 +1296,7 @@ class VLANFilterForm(BootstrapMixin, TenancyFilterForm, CustomFieldFilterForm):
|
||||
null_option=True,
|
||||
)
|
||||
)
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.fields['tag'] = TagFilterField(self.model)
|
||||
tag = TagFilterField(model)
|
||||
|
||||
|
||||
#
|
||||
@ -1372,10 +1357,7 @@ class ServiceFilterForm(BootstrapMixin, CustomFieldFilterForm):
|
||||
port = forms.IntegerField(
|
||||
required=False,
|
||||
)
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.fields['tag'] = TagFilterField(self.model)
|
||||
tag = TagFilterField(model)
|
||||
|
||||
|
||||
class ServiceBulkEditForm(BootstrapMixin, CustomFieldBulkEditForm):
|
||||
|
@ -184,10 +184,7 @@ class SecretFilterForm(BootstrapMixin, CustomFieldFilterForm):
|
||||
value_field="slug",
|
||||
)
|
||||
)
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.fields['tag'] = TagFilterField(self.model)
|
||||
tag = TagFilterField(model)
|
||||
|
||||
|
||||
#
|
||||
|
@ -113,10 +113,7 @@ class TenantFilterForm(BootstrapMixin, CustomFieldFilterForm):
|
||||
null_option=True,
|
||||
)
|
||||
)
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.fields['tag'] = TagFilterField(self.model)
|
||||
tag = TagFilterField(model)
|
||||
|
||||
|
||||
#
|
||||
|
@ -606,11 +606,16 @@ class TagFilterField(forms.MultipleChoiceField):
|
||||
widget = StaticSelect2Multiple
|
||||
|
||||
def __init__(self, model, *args, **kwargs):
|
||||
# Only instanitate the field if the model supports tags (i.e. hide if not)
|
||||
if hasattr(model, 'tags'):
|
||||
tags = model.tags.annotate(count=Count('extras_taggeditem_items')).order_by('name')
|
||||
choices = [(str(tag.slug), '{} ({})'.format(tag.name, tag.count)) for tag in tags]
|
||||
self.model = model
|
||||
|
||||
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):
|
||||
|
@ -216,10 +216,7 @@ class ClusterFilterForm(BootstrapMixin, CustomFieldFilterForm):
|
||||
null_option=True,
|
||||
)
|
||||
)
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.fields['tag'] = TagFilterField(self.model)
|
||||
tag = TagFilterField(model)
|
||||
|
||||
|
||||
class ClusterAddDevicesForm(BootstrapMixin, ChainedFieldsMixin, forms.Form):
|
||||
@ -626,10 +623,7 @@ class VirtualMachineFilterForm(BootstrapMixin, TenancyFilterForm, CustomFieldFil
|
||||
required=False,
|
||||
label='MAC address'
|
||||
)
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.fields['tag'] = TagFilterField(self.model)
|
||||
tag = TagFilterField(model)
|
||||
|
||||
|
||||
#
|
||||
|
Reference in New Issue
Block a user