mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-23 07:56:44 -06:00
Introduce get_indexer() utility function
This commit is contained in:
parent
bfbcc6bbac
commit
7bcc69c043
@ -4,6 +4,7 @@ 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
|
||||
@ -61,9 +62,12 @@ class CachedValue(models.Model):
|
||||
|
||||
@property
|
||||
def display_attrs(self):
|
||||
indexer = registry['search'].get(content_type_identifier(self.object_type))
|
||||
"""
|
||||
Render any display attributes associated with this search result.
|
||||
"""
|
||||
indexer = get_indexer(self.object_type)
|
||||
attrs = {}
|
||||
for attr in getattr(indexer, 'display_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):
|
||||
|
14
netbox/netbox/search/utils.py
Normal file
14
netbox/netbox/search/utils.py
Normal file
@ -0,0 +1,14 @@
|
||||
from netbox.registry import registry
|
||||
from utilities.utils import content_type_identifier
|
||||
|
||||
__all__ = (
|
||||
'get_indexer',
|
||||
)
|
||||
|
||||
|
||||
def get_indexer(content_type):
|
||||
"""
|
||||
Return the registered search indexer for the given ContentType.
|
||||
"""
|
||||
ct_identifier = content_type_identifier(content_type)
|
||||
return registry['search'].get(ct_identifier)
|
Loading…
Reference in New Issue
Block a user