1
0
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:
Saria Hajjar
2020-01-14 08:22:27 +00:00
parent 83ee83142a
commit e10333bf2b
7 changed files with 27 additions and 79 deletions

View File

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