Add vlan filtering on the vm interface like the device #7773

Closed
opened 2025-12-29 20:28:01 +01:00 by adam · 2 comments
Owner

Originally created by @abhi1693 on GitHub (Mar 19, 2023).

Originally assigned to: @abhi1693 on GitHub.

NetBox version

v3.4.6

Feature type

Change to existing functionality

Proposed functionality

diff --git a/netbox/virtualization/filtersets.py b/netbox/virtualization/filtersets.py
index 8f656811a..9a36fa0f6 100644
--- a/netbox/virtualization/filtersets.py
+++ b/netbox/virtualization/filtersets.py
@@ -4,7 +4,7 @@ from django.utils.translation import gettext as _
 
 from dcim.models import Device, DeviceRole, Platform, Region, Site, SiteGroup
 from extras.filtersets import LocalConfigContextFilterSet
-from ipam.models import L2VPN, VRF
+from ipam.models import L2VPN, VLAN, VRF
 from netbox.filtersets import OrganizationalModelFilterSet, NetBoxModelFilterSet
 from tenancy.filtersets import TenancyFilterSet, ContactModelFilterSet
 from utilities.filters import MultiValueCharFilter, MultiValueMACAddressFilter, TreeNodeMultipleChoiceFilter
@@ -308,6 +308,14 @@ class VMInterfaceFilterSet(NetBoxModelFilterSet):
         to_field_name='identifier',
         label=_('L2VPN'),
     )
+    vlan_id = django_filters.CharFilter(
+        method='filter_vlan_id',
+        label=_('Assigned VLAN')
+    )
+    vlan = django_filters.CharFilter(
+        method='filter_vlan',
+        label=_('Assigned VID')
+    )
 
     class Meta:
         model = VMInterface
@@ -320,3 +328,21 @@ class VMInterfaceFilterSet(NetBoxModelFilterSet):
             Q(name__icontains=value) |
             Q(description__icontains=value)
         )
+
+    def filter_vlan_id(self, queryset, name, value):
+        value = value.strip()
+        if not value:
+            return queryset
+        return queryset.filter(
+            Q(untagged_vlan_id=value) |
+            Q(tagged_vlans=value)
+        )
+
+    def filter_vlan(self, queryset, name, value):
+        value = value.strip()
+        if not value:
+            return queryset
+        return queryset.filter(
+            Q(untagged_vlan_id__vid=value) |
+            Q(tagged_vlans__vid=value)
+        )

Use case

Should be able to filter the VLANs by the vm interface

Database changes

No response

External dependencies

No response

Originally created by @abhi1693 on GitHub (Mar 19, 2023). Originally assigned to: @abhi1693 on GitHub. ### NetBox version v3.4.6 ### Feature type Change to existing functionality ### Proposed functionality ``` diff --git a/netbox/virtualization/filtersets.py b/netbox/virtualization/filtersets.py index 8f656811a..9a36fa0f6 100644 --- a/netbox/virtualization/filtersets.py +++ b/netbox/virtualization/filtersets.py @@ -4,7 +4,7 @@ from django.utils.translation import gettext as _ from dcim.models import Device, DeviceRole, Platform, Region, Site, SiteGroup from extras.filtersets import LocalConfigContextFilterSet -from ipam.models import L2VPN, VRF +from ipam.models import L2VPN, VLAN, VRF from netbox.filtersets import OrganizationalModelFilterSet, NetBoxModelFilterSet from tenancy.filtersets import TenancyFilterSet, ContactModelFilterSet from utilities.filters import MultiValueCharFilter, MultiValueMACAddressFilter, TreeNodeMultipleChoiceFilter @@ -308,6 +308,14 @@ class VMInterfaceFilterSet(NetBoxModelFilterSet): to_field_name='identifier', label=_('L2VPN'), ) + vlan_id = django_filters.CharFilter( + method='filter_vlan_id', + label=_('Assigned VLAN') + ) + vlan = django_filters.CharFilter( + method='filter_vlan', + label=_('Assigned VID') + ) class Meta: model = VMInterface @@ -320,3 +328,21 @@ class VMInterfaceFilterSet(NetBoxModelFilterSet): Q(name__icontains=value) | Q(description__icontains=value) ) + + def filter_vlan_id(self, queryset, name, value): + value = value.strip() + if not value: + return queryset + return queryset.filter( + Q(untagged_vlan_id=value) | + Q(tagged_vlans=value) + ) + + def filter_vlan(self, queryset, name, value): + value = value.strip() + if not value: + return queryset + return queryset.filter( + Q(untagged_vlan_id__vid=value) | + Q(tagged_vlans__vid=value) + ) ``` ### Use case Should be able to filter the VLANs by the vm interface ### Database changes _No response_ ### External dependencies _No response_
adam added the status: acceptedtype: feature labels 2025-12-29 20:28:01 +01:00
adam closed this issue 2025-12-29 20:28:01 +01:00
Author
Owner

@jeremystretch commented on GitHub (Mar 20, 2023):

Rather than replicating the existing filters, they should be refactors into a common class that can be inherited from both the DCIM and VM interface filter sets.

@jeremystretch commented on GitHub (Mar 20, 2023): Rather than replicating the existing filters, they should be refactors into a common class that can be inherited from both the DCIM and VM interface filter sets.
Author
Owner

@abhi1693 commented on GitHub (Mar 20, 2023):

I can do that

@abhi1693 commented on GitHub (Mar 20, 2023): I can do that
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#7773