Closes #14134: Display additional object attributes in global search results (#14154)

* WIP

* Add display_attrs for all indexers

* Linkify object attributes

* Clean up prefetch logic

* Use tooltips for display attributes

* Simplify template code

* Introduce get_indexer() utility function

* Add  to examples in docs

* Use tooltips to display long strings
This commit is contained in:
Jeremy Stretch
2023-11-09 16:21:09 -05:00
committed by GitHub
parent 2562c8745c
commit 3d20276f55
15 changed files with 165 additions and 7 deletions
+19
View File
@@ -4,7 +4,10 @@ from django.contrib.contenttypes.models import ContentType
from django.db import models
from django.utils.translation import gettext_lazy as _
from netbox.search.utils import get_indexer
from netbox.registry import registry
from utilities.fields import RestrictedGenericForeignKey
from utilities.utils import content_type_identifier
from ..fields import CachedValueField
__all__ = (
@@ -58,3 +61,19 @@ class CachedValue(models.Model):
def __str__(self):
return f'{self.object_type} {self.object_id}: {self.field}={self.value}'
@property
def display_attrs(self):
"""
Render any display attributes associated with this search result.
"""
indexer = get_indexer(self.object_type)
attrs = {}
for attr in indexer.display_attrs:
name = self.object._meta.get_field(attr).verbose_name
if value := getattr(self.object, attr):
if display_func := getattr(self.object, f'get_{attr}_display', None):
attrs[name] = display_func()
else:
attrs[name] = value
return attrs