From 6a06b54348aa88cdcd99f0e6900a20f9ff0cf74b Mon Sep 17 00:00:00 2001 From: Prince Kumar Date: Mon, 16 Oct 2023 18:21:03 +0530 Subject: [PATCH] Add dns_name filter on the IP Address page #13957 --- netbox/ipam/filtersets.py | 17 ++++++++++++++++- netbox/ipam/forms/filtersets.py | 11 ++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/netbox/ipam/filtersets.py b/netbox/ipam/filtersets.py index bc918128651..332eb43577c 100644 --- a/netbox/ipam/filtersets.py +++ b/netbox/ipam/filtersets.py @@ -618,10 +618,14 @@ class IPAddressFilterSet(NetBoxModelFilterSet, TenancyFilterSet): role = django_filters.MultipleChoiceFilter( choices=IPAddressRoleChoices ) + dns_name = MultiValueCharFilter( + method='search_by_dns', + label=_('DNS Name'), + ) class Meta: model = IPAddress - fields = ['id', 'dns_name', 'description'] + fields = ['id', 'description'] def search(self, queryset, name, value): if not value.strip(): @@ -738,6 +742,17 @@ def _assigned(self, queryset, name, value): assigned_object_id__isnull=True ) + def search_by_dns(self, queryset, name, value): + if not value: + return queryset + + normalized_value = str(value[0]).lower() + + if normalized_value in ('none', 'null'): + return queryset.filter(dns_name='') + + return queryset.filter(dns_name__in=value) + class FHRPGroupFilterSet(NetBoxModelFilterSet): protocol = django_filters.MultipleChoiceFilter( diff --git a/netbox/ipam/forms/filtersets.py b/netbox/ipam/forms/filtersets.py index e4e967f8117..120044937ee 100644 --- a/netbox/ipam/forms/filtersets.py +++ b/netbox/ipam/forms/filtersets.py @@ -295,7 +295,7 @@ class IPAddressFilterForm(TenancyFilterForm, NetBoxModelFilterSetForm): model = IPAddress fieldsets = ( (None, ('q', 'filter_id', 'tag')), - (_('Attributes'), ('parent', 'family', 'status', 'role', 'mask_length', 'assigned_to_interface')), + (_('Attributes'), ('parent', 'family', 'status', 'role', 'mask_length', 'assigned_to_interface', 'dns_name')), (_('VRF'), ('vrf_id', 'present_in_vrf_id')), (_('Tenant'), ('tenant_group_id', 'tenant_id')), (_('Device/VM'), ('device_id', 'virtual_machine_id')), @@ -357,6 +357,15 @@ class IPAddressFilterForm(TenancyFilterForm, NetBoxModelFilterSetForm): choices=BOOLEAN_WITH_BLANK_CHOICES ) ) + dns_name = forms.CharField( + required=False, + widget=forms.TextInput( + attrs={ + 'placeholder': 'DNS Name', + } + ), + label='DNS Name' + ) tag = TagFilterField(model)