mirror of
https://github.com/netbox-community/netbox.git
synced 2026-02-05 23:06:25 -06:00
* 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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user