Coverage for netbox/forms/filtersets.py: 90%
20 statements
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-10 18:35 +0000
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-10 18:35 +0000
1from django import forms
2from django.utils.translation import gettext_lazy as _
4from extras.choices import *
5from utilities.forms.fields import QueryField
6from utilities.forms.mixins import FilterModifierMixin
8from .mixins import CustomFieldsMixin, OwnerFilterMixin, SavedFiltersMixin
10__all__ = (
11 'NestedGroupModelFilterSetForm',
12 'NetBoxModelFilterSetForm',
13 'OrganizationalModelFilterSetForm',
14 'PrimaryModelFilterSetForm',
15)
18class NetBoxModelFilterSetForm(FilterModifierMixin, CustomFieldsMixin, SavedFiltersMixin, forms.Form):
19 """
20 Base form for FilerSet forms. These are used to filter object lists in the NetBox UI. Note that the
21 corresponding FilterSet *must* provide a `q` filter.
23 Attributes:
24 model: The model class associated with the form
25 fieldsets: An iterable of two-tuples which define a heading and field set to display per section of
26 the rendered form (optional). If not defined, the all fields will be rendered as a single section.
27 selector_fields: An iterable of names of fields to display by default when rendering the form as
28 a selector widget
29 """
30 q = QueryField(
31 required=False,
32 label=_('Search')
33 )
35 selector_fields = ('filter_id', 'q')
37 def _get_custom_fields(self, content_type):
38 # Return only non-hidden custom fields for which filtering is enabled (excluding JSON fields)
39 return [
40 cf for cf in super()._get_custom_fields(content_type) if (
41 cf.filter_logic != CustomFieldFilterLogicChoices.FILTER_DISABLED and
42 cf.type != CustomFieldTypeChoices.TYPE_JSON
43 )
44 ]
46 def _get_form_field(self, customfield):
47 return customfield.to_form_field(
48 set_initial=False, enforce_required=False, enforce_visibility=False, for_filterset_form=True
49 )
52class PrimaryModelFilterSetForm(OwnerFilterMixin, NetBoxModelFilterSetForm):
53 """
54 FilterSet form for models which inherit from PrimaryModel.
55 """
56 pass
59class OrganizationalModelFilterSetForm(OwnerFilterMixin, NetBoxModelFilterSetForm):
60 """
61 FilterSet form for models which inherit from OrganizationalModel.
62 """
63 pass
66class NestedGroupModelFilterSetForm(OwnerFilterMixin, NetBoxModelFilterSetForm):
67 """
68 FilterSet form for models which inherit from NestedGroupModel.
69 """
70 pass