Skip to content

搜索

插件可以定义和注册自己的模型来扩展NetBox的核心搜索功能。通常,插件将包含一个名为search.py的文件,其中包含其模型的所有搜索索引(请参阅下面的示例)。

# search.py
from netbox.search import SearchIndex
from .models import MyModel

class MyModelIndex(SearchIndex):
    model = MyModel
    fields = (
        ('name', 100),
        ('description', 500),
        ('comments', 5000),
    )
    display_attrs = ('site', 'device', 'status', 'description')

display_attrs 中列出的字段不会被缓存用于搜索,但将在全局搜索结果中与对象一起显示。这有助于向用户传达有关对象的其他信息。

要将一个或多个索引注册到NetBox,请在此文件末尾定义一个名为indexes的列表:

indexes = [MyModelIndex]

提示

可以通过在PluginConfig实例中设置search_indexes来修改搜索索引列表的路径。

SearchIndex

Base class for building search indexes.

Attributes:

Name Type Description
model

The model class for which this index is used.

category

The label of the group under which this indexer is categorized (for form field display). If none, the name of the model's app will be used.

fields

An iterable of two-tuples defining the model fields to be indexed and the weight associated with each.

display_attrs

An iterable of additional object attributes to include when displaying search results.

get_field_type(instance, field_name) staticmethod

Return the data type of the specified model field.

get_field_value(instance, field_name) staticmethod

Return the value of the specified model field as a string.

to_cache(instance, custom_fields=None) classmethod

Return a list of ObjectFieldValue representing the instance fields to be cached.

Parameters:

Name Type Description Default
instance

The instance being cached.

required
custom_fields

An iterable of CustomFields to include when caching the instance. If None, all custom fields defined for the model will be included. (This can also be provided during bulk caching to avoid looking up the available custom fields for each instance.)

None