Skip to content

表格

NetBox使用django-tables2库来渲染动态对象表格。这些表格显示对象列表,并可以根据各种参数进行排序和过滤。

NetBoxTable

为了提供比django-tables2中的默认Table类支持的功能更多的功能,NetBox提供了NetBoxTable类。这个自定义表格类支持以下功能:

  • 用户可配置的列显示和排序
  • 自定义字段和自定义链接列
  • 相关对象的自动预取

它还包括一些默认列:

  • pk - 用于选择与每个表格行相关联的对象的复选框(如果适用)
  • id - 对象的数值数据库ID,作为指向对象视图的超链接(默认情况下隐藏)
  • actions - 一个下拉菜单,向用户呈现可用于对象的特定操作

示例

# tables.py
import django_tables2 as tables
from netbox.tables import NetBoxTable
from .models import MyModel

class MyModelTable(NetBoxTable):
    name = tables.Column(
        linkify=True
    )
    ...

    class Meta(NetBoxTable.Meta):
        model = MyModel
        fields = ('pk', 'id', 'name', ...)
        default_columns = ('pk', 'name', ...)

表格配置

NetBoxTable类具有动态配置功能,允许用户更改其列显示和排序首选项。要为特定请求配置表格,只需调用其configure()方法并传递当前的HTTPRequest对象。例如:

table = MyModelTable(data=MyModel.objects.all())
table.configure(request)

这将自动应用表格的任何用户特定首选项。(如果使用NetBox提供的通用视图,表格配置将自动处理。)

下面列出的表格列类可用于插件。这些类可以从netbox.tables.columns导入。

BooleanColumn (Column)

Custom implementation of BooleanColumn to render a nicely-formatted checkmark or X icon instead of a Unicode character.

ChoiceFieldColumn (Column)

Render a model's static ChoiceField with its value from get_FOO_display() as a colored badge. Background color is set by the instance's get_FOO_color() method, if defined.

ColorColumn (Column)

Display an arbitrary color value, specified in RRGGBB format.

ColoredLabelColumn (TemplateColumn)

Render a related object as a colored label. The related object must have a color attribute (specifying an RRGGBB value) and a get_absolute_url() method.

ContentTypeColumn (Column)

Display a ContentType instance.

ContentTypesColumn (ManyToManyColumn)

Display a list of ContentType instances.

MarkdownColumn (TemplateColumn)

Render a Markdown string.

TagColumn (TemplateColumn)

Display a list of Tags assigned to the object.

TemplateColumn (TemplateColumn)

Overrides django-tables2's stock TemplateColumn class to render a placeholder symbol if the returned value is an empty string.

__init__(self, export_raw=False, **kwargs) special

Parameters:

Name Type Description Default
export_raw

If true, data export returns the raw field value rather than the rendered template. (Default: False)

False

扩展核心表格

此功能在NetBox v3.7中引入。

插件可以使用register_table_column()实用程序函数在核心表格上注册自己的自定义列。这允许插件附加额外的信息,例如与自己的模型的关系,到内置对象列表。

import django_tables2
from django.utils.translation import gettext_lazy as _

from dcim.tables import SiteTable
from utilities.tables import register_table_column

mycol = django_tables2.Column(
    verbose_name=_('My Column'),
    accessor=django_tables2.A('description')
)

register_table_column(mycol, 'foo', SiteTable)

通常,在定义自定义列时,您会想要定义一个访问器,以识别所需的模型字段或关系。有关创建自定义列的更多信息,请参阅django-tables2文档

register_table_column(column, name, *tables)

Register a custom column for use on one or more tables.

Parameters:

Name Type Description Default
column

The column instance to register

required
name

The name of the table column

required
tables

One or more table classes

()