Add additional filtering to MAC Addresses #11640

Closed
opened 2025-12-29 21:47:56 +01:00 by adam · 2 comments
Owner

Originally created by @jvalente-salemstate on GitHub (Sep 19, 2025).

Originally assigned to: @pheus on GitHub.

NetBox version

v4.4.0

Feature type

Change to existing functionality

Proposed functionality

Currently the MAC Address view only allows filtering on:

  • MAC Address
  • Assigned Device
  • Assigned VM

This makes searching and filtering through (possibles tens of) thousands of MAC addresses quite a lot.

Additional filtering would be very helpful for searching and using export templates.

I do not believe any model changes are needed, but changes would need to be made to the list view and MACAddressFilterSet

class MACAddressFilterSet(NetBoxModelFilterSet):
    mac_address = MultiValueMACAddressFilter()
    device = MultiValueCharFilter(
        method='filter_device',
        field_name='name',
        label=_('Device (name)'),
    )
    device_id = MultiValueNumberFilter(
        method='filter_device',
        field_name='pk',
        label=_('Device (ID)'),
    )
    virtual_machine = MultiValueCharFilter(
        method='filter_virtual_machine',
        field_name='name',
        label=_('Virtual machine (name)'),
    )
    virtual_machine_id = MultiValueNumberFilter(
        method='filter_virtual_machine',
        field_name='pk',
        label=_('Virtual machine (ID)'),
    )
    interface = django_filters.ModelMultipleChoiceFilter(
        field_name='interface__name',
        queryset=Interface.objects.all(),
        to_field_name='name',
        label=_('Interface (name)'),
    )
    interface_id = django_filters.ModelMultipleChoiceFilter(
        field_name='interface',
        queryset=Interface.objects.all(),
        label=_('Interface (ID)'),
    )
    vminterface = django_filters.ModelMultipleChoiceFilter(
        field_name='vminterface__name',
        queryset=VMInterface.objects.all(),
        to_field_name='name',
        label=_('VM interface (name)'),
    )
    vminterface_id = django_filters.ModelMultipleChoiceFilter(
        field_name='vminterface',
        queryset=VMInterface.objects.all(),
        label=_('VM interface (ID)'),
    )

Use case

Filter Use Case
Assigned: true/false Filtering for only (un)assigned MACs
Is Primary: true/false Show only/do not show primary MAC addresses
Parent Device Role Filter on MAC for switches, firewalls, cameras, etc
Parent interface connected/Cable: true/false Show/don't show MACs assigned to connected/cabled interfaces
Parent device tenant or site/location Show only MACs for a given tenant, site, or location

Database changes

No response

External dependencies

No response

Originally created by @jvalente-salemstate on GitHub (Sep 19, 2025). Originally assigned to: @pheus on GitHub. ### NetBox version v4.4.0 ### Feature type Change to existing functionality ### Proposed functionality Currently the MAC Address view only allows filtering on: * MAC Address * Assigned Device * Assigned VM This makes searching and filtering through (possibles tens of) thousands of MAC addresses quite a lot. Additional filtering would be very helpful for searching and using export templates. I do not believe any model changes are needed, but changes would need to be made to the list view and [`MACAddressFilterSet`](https://github.com/netbox-community/netbox/blob/a173a9b4acc4057f694210315e3083607f1b8800/netbox/dcim/filtersets.py#L1765-L1809) ``` class MACAddressFilterSet(NetBoxModelFilterSet): mac_address = MultiValueMACAddressFilter() device = MultiValueCharFilter( method='filter_device', field_name='name', label=_('Device (name)'), ) device_id = MultiValueNumberFilter( method='filter_device', field_name='pk', label=_('Device (ID)'), ) virtual_machine = MultiValueCharFilter( method='filter_virtual_machine', field_name='name', label=_('Virtual machine (name)'), ) virtual_machine_id = MultiValueNumberFilter( method='filter_virtual_machine', field_name='pk', label=_('Virtual machine (ID)'), ) interface = django_filters.ModelMultipleChoiceFilter( field_name='interface__name', queryset=Interface.objects.all(), to_field_name='name', label=_('Interface (name)'), ) interface_id = django_filters.ModelMultipleChoiceFilter( field_name='interface', queryset=Interface.objects.all(), label=_('Interface (ID)'), ) vminterface = django_filters.ModelMultipleChoiceFilter( field_name='vminterface__name', queryset=VMInterface.objects.all(), to_field_name='name', label=_('VM interface (name)'), ) vminterface_id = django_filters.ModelMultipleChoiceFilter( field_name='vminterface', queryset=VMInterface.objects.all(), label=_('VM interface (ID)'), ) ``` ### Use case |Filter| Use Case | | --- | --- | |Assigned: `true/false` | Filtering for only (un)assigned MACs| |Is Primary: `true/false` | Show only/do not show primary MAC addresses| |Parent Device Role | Filter on MAC for switches, firewalls, cameras, etc| |Parent interface connected/Cable: `true/false`|Show/don't show MACs assigned to connected/cabled interfaces| |Parent device tenant or site/location|Show only MACs for a given tenant, site, or location| ### Database changes _No response_ ### External dependencies _No response_
adam added the status: acceptedtype: featurecomplexity: mediumnetbox labels 2025-12-29 21:47:56 +01:00
adam closed this issue 2025-12-29 21:47:56 +01:00
Author
Owner

@jnovinger commented on GitHub (Oct 2, 2025):

Thanks for the suggestion, @jvalente-salemstate . We can add filtering for MAC addresses that are directly on the model itself, but we need to avoid filters that traverse multiple relationships as this can lead to performance issues and query complexity. Even single-degree relationships require careful consideration.

We'll accept:

  • assigned (true/false) - Filter for assigned vs. unassigned MAC addresses
  • is_primary (true/false) - Show only/exclude primary MAC addresses (note: the implementation of this filter will likely be tricky due to how primary MAC addresses need to be determined)

We're declining:

  • Parent device role, tenant, site, location - These are 2+ relationship hops away from the MAC address model. You can achieve this by filtering on the device or interface lists instead, then accessing their MAC addresses.
  • Parent interface connected/cable status - While this is only one relationship away, it adds complexity. You can filter interfaces by cable status and access their MAC addresses through that path.
@jnovinger commented on GitHub (Oct 2, 2025): Thanks for the suggestion, @jvalente-salemstate . We can add filtering for MAC addresses that are directly on the model itself, but we need to avoid filters that traverse multiple relationships as this can lead to performance issues and query complexity. Even single-degree relationships require careful consideration. We'll accept: - `assigned` (true/false) - Filter for assigned vs. unassigned MAC addresses - `is_primary` (true/false) - Show only/exclude primary MAC addresses (note: the implementation of this filter will likely be tricky due to how primary MAC addresses need to be determined) We're declining: - Parent device role, tenant, site, location - These are 2+ relationship hops away from the MAC address model. You can achieve this by filtering on the device or interface lists instead, then accessing their MAC addresses. - Parent interface connected/cable status - While this is only one relationship away, it adds complexity. You can filter interfaces by cable status and access their MAC addresses through that path.
Author
Owner

@pheus commented on GitHub (Oct 19, 2025):

Hi all!
I think I have a good approach for this and would love to take it on. If that sounds good, could you please assign the issue to me? I’ll also open a draft PR so we can discuss the approach early and iterate on any feedback.

Plan / scope

  • Add two boolean filters on MACAddress: assigned and primary.
  • Keep naming consistent with other boolean filters; I’m proposing assigned and primary, but I’m happy to switch to is_assigned / is_primary if that better matches project conventions.
  • Ensure parity across REST and GraphQL.
  • Include two unit tests.

Behavior (at a glance)

  • assigned: true ⇒ MACs bound to any Interface/VMInterface; false ⇒ unassigned.
  • primary: true ⇒ MACs set as the primary MAC on any Interface/VMInterface; false ⇒ all others.

If you have a preference on naming or any edge cases you want covered, I’m happy to adjust. Thanks!

@pheus commented on GitHub (Oct 19, 2025): Hi all! I think I have a good approach for this and would love to take it on. If that sounds good, could you please assign the issue to me? I’ll also open a **draft PR** so we can discuss the approach early and iterate on any feedback. **Plan / scope** - Add two boolean filters on `MACAddress`: `assigned` and `primary`. - Keep naming consistent with other boolean filters; I’m proposing `assigned` and `primary`, but I’m happy to switch to `is_assigned` / `is_primary` if that better matches project conventions. - Ensure **parity across REST and GraphQL**. - Include two unit tests. **Behavior (at a glance)** - `assigned`: `true` ⇒ MACs bound to any `Interface`/`VMInterface`; `false` ⇒ unassigned. - `primary`: `true` ⇒ MACs set as the primary MAC on any `Interface`/`VMInterface`; `false` ⇒ all others. If you have a preference on naming or any edge cases you want covered, I’m happy to adjust. Thanks!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#11640