From 409f63c92297be1008195d3b1dce4b2d19cf129f Mon Sep 17 00:00:00 2001 From: Arthur Date: Tue, 16 Jun 2026 15:02:16 -0700 Subject: [PATCH] optimize mac address check --- netbox/dcim/models/devices.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/netbox/dcim/models/devices.py b/netbox/dcim/models/devices.py index faac46c39..8da87f8f8 100644 --- a/netbox/dcim/models/devices.py +++ b/netbox/dcim/models/devices.py @@ -1407,9 +1407,10 @@ class MACAddress(PrimaryModel): @cached_property def is_primary(self): - if self.assigned_object and hasattr(self.assigned_object, 'primary_mac_address'): - if self.assigned_object.primary_mac_address and self.assigned_object.primary_mac_address.pk == self.pk: - return True + # Compare against primary_mac_address_id (a column already loaded on the assigned object) rather than + # dereferencing primary_mac_address, to avoid an extra query per object in list responses. + if (obj := self.assigned_object) is not None and hasattr(obj, 'primary_mac_address_id'): + return obj.primary_mac_address_id == self.pk return False def clean(self, *args, **kwargs):