mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-23 04:22:01 -06:00
Fixes #557: Add 'global' choice to VRF filter for prefixes and IP addresses
This commit is contained in:
parent
2567412121
commit
daadf7a49b
@ -272,7 +272,8 @@ class PrefixFilterForm(BootstrapMixin, CustomFieldFilterForm):
|
|||||||
'placeholder': 'Network',
|
'placeholder': 'Network',
|
||||||
}))
|
}))
|
||||||
family = forms.ChoiceField(required=False, choices=IP_FAMILY_CHOICES, label='Address Family')
|
family = forms.ChoiceField(required=False, choices=IP_FAMILY_CHOICES, label='Address Family')
|
||||||
vrf = FilterChoiceField(choices=get_filter_choices(VRF, count_field='prefixes'), label='VRF')
|
vrf = FilterChoiceField(choices=get_filter_choices(VRF, count_field='prefixes', null_option=(0, 'Global')),
|
||||||
|
label='VRF')
|
||||||
tenant = FilterChoiceField(choices=get_filter_choices(Tenant, id_field='slug', count_field='prefixes'),
|
tenant = FilterChoiceField(choices=get_filter_choices(Tenant, id_field='slug', count_field='prefixes'),
|
||||||
label='Tenant')
|
label='Tenant')
|
||||||
status = FilterChoiceField(choices=prefix_status_choices)
|
status = FilterChoiceField(choices=prefix_status_choices)
|
||||||
|
@ -35,24 +35,27 @@ def add_blank_choice(choices):
|
|||||||
return ((None, '---------'),) + choices
|
return ((None, '---------'),) + choices
|
||||||
|
|
||||||
|
|
||||||
def get_filter_choices(model, id_field='pk', select_related=[], count_field=None):
|
def get_filter_choices(model, id_field='pk', select_related=[], count_field=None, null_option=None):
|
||||||
"""
|
"""
|
||||||
Return a list of choices suitable for a ChoiceField.
|
Return a list of choices suitable for a ChoiceField.
|
||||||
|
|
||||||
:param model: The base model to use for the queryset
|
:param model: The base model to use for the queryset
|
||||||
:param id_field: Field to use as the object identifier
|
:param id_field: Field to use as the object identifier
|
||||||
:param select_related: Any related tables to include
|
:param select_related: Any related tables to include
|
||||||
:param count: The field to use for a child COUNT() (optional)
|
:param count_field: The field to use for a child COUNT() (optional)
|
||||||
:return:
|
:param null_option: A (value, label) tuple to include at the beginning of the list serving as "null"
|
||||||
"""
|
"""
|
||||||
queryset = model.objects.all()
|
queryset = model.objects.all()
|
||||||
if select_related:
|
if select_related:
|
||||||
queryset = queryset.select_related(*select_related)
|
queryset = queryset.select_related(*select_related)
|
||||||
if count_field:
|
if count_field:
|
||||||
queryset = queryset.annotate(child_count=Count(count_field))
|
queryset = queryset.annotate(child_count=Count(count_field))
|
||||||
return [(getattr(obj, id_field), u'{} ({})'.format(obj, obj.child_count)) for obj in queryset]
|
choices = [(getattr(obj, id_field), u'{} ({})'.format(obj, obj.child_count)) for obj in queryset]
|
||||||
else:
|
else:
|
||||||
return [(getattr(obj, id_field), u'{}'.format(obj)) for obj in queryset]
|
choices = [(getattr(obj, id_field), u'{}'.format(obj)) for obj in queryset]
|
||||||
|
if null_option:
|
||||||
|
choices = [null_option] + choices
|
||||||
|
return choices
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user