Inconsistent ordering of interfaces in virtual chassis #3830

Closed
opened 2025-12-29 18:31:24 +01:00 by adam · 1 comment
Owner

Originally created by @steffann on GitHub (Jul 2, 2020).

Originally assigned to: @steffann on GitHub.

Environment

  • Python version: 3.6.9
  • NetBox version: 2.8.7

Steps to Reproduce

  1. Create a device named "Demo 1"
  2. Create an interface named "demo-2" on this device
  3. Create a device named "Demo 2"
  4. Create an interface named "demo-1" on this device
  5. Combine these devices into a virtual chassis (Device "Demo1" is the master and has position 1, device "Demo 2" has position 2)
  6. View device "Demo 1"

Expected Behavior

I expected the interfaces to be ordered:

  • demo-1
  • demo-2

Observed Behavior

The interfaces are ordered:

  • demo-2
  • demo-1

Analysis

The default ordering for interfaces is ordering = ('device', CollateAsChar('_name')). This causes interfaces to be ordered first by device name, even when viewing vc_interfaces.

Suggested solution

Because when retrieving interfaces through vc_interfaces it returns the interfaces as if they all belong the the master device, the name of the actual device should be ignored. This can be accomplished by explicitly sorting the interfaces by _name in vc_interfaces:

@property
def vc_interfaces(self):
    """
    Return a QuerySet matching all Interfaces assigned to this Device or, if this Device is a VC master, to another
    Device belonging to the same VirtualChassis.
    """
    filter = Q(device=self)
    if self.virtual_chassis and self.virtual_chassis.master == self:
        filter |= Q(device__virtual_chassis=self.virtual_chassis, mgmt_only=False)
    return Interface.objects.filter(filter).order_by(CollateAsChar('_name'))
Originally created by @steffann on GitHub (Jul 2, 2020). Originally assigned to: @steffann on GitHub. ### Environment * Python version: 3.6.9 * NetBox version: 2.8.7 ### Steps to Reproduce 1. Create a device named "Demo 1" 2. Create an interface named "demo-2" on this device 3. Create a device named "Demo 2" 4. Create an interface named "demo-1" on this device 5. Combine these devices into a virtual chassis (Device "Demo1" is the master and has position 1, device "Demo 2" has position 2) 6. View device "Demo 1" ### Expected Behavior I expected the interfaces to be ordered: - demo-1 - demo-2 ### Observed Behavior The interfaces are ordered: - demo-2 - demo-1 ### Analysis The default ordering for interfaces is `ordering = ('device', CollateAsChar('_name'))`. This causes interfaces to be ordered first by device name, even when viewing `vc_interfaces`. ### Suggested solution Because when retrieving interfaces through `vc_interfaces` it returns the interfaces as if they all belong the the master device, the name of the actual device should be ignored. This can be accomplished by explicitly sorting the interfaces by `_name` in `vc_interfaces`: ```python @property def vc_interfaces(self): """ Return a QuerySet matching all Interfaces assigned to this Device or, if this Device is a VC master, to another Device belonging to the same VirtualChassis. """ filter = Q(device=self) if self.virtual_chassis and self.virtual_chassis.master == self: filter |= Q(device__virtual_chassis=self.virtual_chassis, mgmt_only=False) return Interface.objects.filter(filter).order_by(CollateAsChar('_name')) ```
adam added the pending closure label 2025-12-29 18:31:24 +01:00
adam closed this issue 2025-12-29 18:31:25 +01:00
Author
Owner

@steffann commented on GitHub (Jul 2, 2020):

This is considered expected behaviour, so will not be changed

@steffann commented on GitHub (Jul 2, 2020): This is considered expected behaviour, so will not be changed
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#3830