From fde7847266ee558b8413b0ef37fda1df66b2a385 Mon Sep 17 00:00:00 2001 From: Ben White Date: Fri, 14 Aug 2026 14:36:12 +0100 Subject: [PATCH] insert fields marked as 'context' into the SearchIndex results --- netbox_custom_objects/models.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/netbox_custom_objects/models.py b/netbox_custom_objects/models.py index 3b1689ca..6643d082 100644 --- a/netbox_custom_objects/models.py +++ b/netbox_custom_objects/models.py @@ -1638,10 +1638,26 @@ def register_custom_object_search_index(self, model): continue fields.append((field.name, field.search_weight)) + # Gather fields marked with context=True + context_field_names = tuple( + f.name for f in self.fields.filter(context=True) if f.name in present + ) + + # Custom get_display_attrs method injected into the dynamic SearchIndex class + @classmethod + def get_display_attrs(cls, instance): + attrs = {} + for field_name in context_field_names: + val = getattr(instance, field_name, None) + if val is not None and val != "": + attrs[field_name] = str(val) + return attrs + attrs = { "model": model, "fields": tuple(fields), - "display_attrs": tuple(), + "display_attrs": context_field_names, + "get_display_attrs": get_display_attrs, } search_index = type( f"{self.name}SearchIndex",