Skip to content

# `nautobot.apps.tables`

Utilities for apps to implement data tables.

## `nautobot.apps.tables.BaseTable`

Bases: `Table`

Default table for object lists

:param user: Personalize table display for the given user (optional). Has no effect if AnonymousUser is passed.

### `__init__(*args, table_changes_pending=False, saved_view=None, user=None, hide_hierarchy_ui=False, order_by=None, data_transform_callback=None, **kwargs)`

Instantiate a BaseTable.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`*args` | `list` | Passed through to django_tables2.Table | `()`
`table_changes_pending` | `bool` | TODO | `False`
`saved_view` | `SavedView` | TODO | `None`
`user` | `User` | TODO | `None`
`hide_hierarchy_ui` | `bool` | Whether to display or hide hierarchy indentation of nested objects. | `False`
`order_by` | `list` | Field(s) to sort by | `None`
`data_transform_callback` | `function` | A function that takes the given `data` as an input and returns new data. Runs after all of the queryset auto-optimization performed by this class. Used for example in IPAM views to inject "fake" records for "available" Prefixes, IPAddresses, or VLANs. | `None`
`**kwargs` | `dict` | Passed through to django_tables2.Table | `{}`

Warning: Do not modify/set the `base_columns` attribute after BaseTable class is instantiated. Do not modify/set the `base_columns` attribute after calling super().init() of BaseTable class.

### `add_conditional_prefetch(table_field, db_column=None, prefetch=None)`

Conditionally prefetch the specified database column if the related table field is visible.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`table_field` | `str` | Name of the field on the table to check for visibility. Also used as the prefetch field if neither db_column nor prefetch is specified. | required
`db_column` | `str` | Optionally specify the db column to prefetch. Mutually exclusive with prefetch. | `None`
`prefetch` | `Prefetch` | Optionally specify a prefetch object. Mutually exclusive with db_column. | `None`

## `nautobot.apps.tables.BooleanColumn`

Bases: `Column`

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

## `nautobot.apps.tables.ButtonsColumn`

Bases: `TemplateColumn`

Render edit, delete, and changelog buttons for an object.

:param model: Model class to use for calculating URL view names :param prepend_template: Additional template content to render in the column (optional) :param return_url_extra: String to append to the return URL (e.g. for specifying a tab) (optional)

## `nautobot.apps.tables.ChoiceFieldColumn`

Bases: `Column`

Render a ChoiceField value inside a indicating a particular CSS class. This is useful for displaying colored choices. The CSS class is derived by calling .get_FOO_class() on the row record.

## `nautobot.apps.tables.ColorColumn`

Bases: `Column`

Display a color (#RRGGBB).

## `nautobot.apps.tables.ColoredLabelColumn`

Bases: `TemplateColumn`

Render a colored label (e.g. for DeviceRoles).

## `nautobot.apps.tables.ComputedFieldColumn`

Bases: `Column`

Display computed fields in the appropriate format.

## `nautobot.apps.tables.ContentTypesColumn`

Bases: `ManyToManyColumn`

Display a list of `content_types` m2m assigned to an object.

Default sorting of content-types is by pk. This sorting comes at a per-row performance hit to querysets for table views. If this becomes an issue, set `sort_items=False`.

:param sort_items: Whether to sort by `(app_label, name)`. (default: True) :param truncate_words: Number of words at which to truncate, or `None` to disable. (default: None)

### `filter(qs)`

Overload filter to optionally sort items.

### `render(value)`

Overload render to optionally truncate words.

## `nautobot.apps.tables.CustomFieldColumn`

Bases: `Column`

Display custom fields in the appropriate format.

## `nautobot.apps.tables.LinkedCountColumn`

Bases: `Column`

Render a count of related objects linked to a filtered URL, or if a single related object is present, the object.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`viewname` | `str` | The list view name to use for URL resolution, for example `"dcim:location_list"` | required
`url_params` | `dict` | Query parameters to apply to filter the list URL (e.g. `{"vlans": "pk"}` will add `?vlans=<record.pk>` to the linked list URL) | `None`
`view_kwargs` | `dict` | Additional kwargs to pass to `reverse()` for list URL resolution. Rarely used. | `None`
`lookup` | `str` | The field name on the base record that can be used to query the related objects. If not specified, `nautobot.core.utils.lookup.get_related_field_for_models()` will be called at render time to attempt to intelligently find the appropriate field. TODO: this currently does not support nested lookups via `__`. That may be solvable in the future. | `None`
`reverse_lookup` | `str` | The reverse lookup parameter to use to derive the count. If not specified, the first key in `url_params` will be implicitly used as the `reverse_lookup` value. | `None`
`distinct` | `bool` | Parameter passed through to `count_related()`. | `False`
`display_field` | `str` | Name of the field to use when displaying an object rather than just a count. This will be passed to hyperlinked_object() as the `field` parameter If not specified, it will use the "display" field. | `'display'`
`**kwargs` | `dict` | As the parent Column class. | `{}`

Examples:

    class VLANTable(..., BaseTable):
        ...
        location_count = LinkedCountColumn(
            # Link for N related locations will be reverse("dcim:location_list") + "?vlans=<record.pk>"
            viewname="dcim:location_list",
            url_params={"vlans": "pk"},
            verbose_name="Locations",
        )

    class CloudNetworkTable(BaseTable):
        ...
        circuit_count = LinkedCountColumn(
            # Link for N related circuits will be reverse("circuits:circuit_list") + "?cloud_network=<record.name>"
            viewname="circuits:circuit_list",
            url_params={"cloud_network": "name"},
            # We'd like to do the below but this module isn't currently smart enough to build the right Prefetch()
            # for a nested lookup:
            # lookup="circuit_terminations__circuit",
            # For the count,
            # .annotate(circuit_count=count_related(Circuit, "circuit_terminations__cloud_network", distinct=True))
            reverse_lookup="circuit_terminations__cloud_network",
            distinct=True,
            verbose_name="Circuits",
        )

## `nautobot.apps.tables.RelationshipColumn`

Bases: `Column`

Display relationship association instances in the appropriate format.

## `nautobot.apps.tables.RoleTableMixin`

Bases: `BaseTable`

Mixin to add a `role` field to a table.

## `nautobot.apps.tables.StatusTableMixin`

Bases: `BaseTable`

Mixin to add a `status` field to a table.

## `nautobot.apps.tables.TableExtension`

Template class for extending Tables.

An app can override the default columns for a table by either: - Extending the original default columns to include custom columns. - add_to_default_columns = ("my_app_name_new_column",) - Removing native columns from the default columns. - remove_from_default_columns = ("tenant",)

### `alter_queryset(queryset)` `classmethod`

Alter the View class QuerySet.

This is a good place to add `prefetch_related` to the view queryset. example: return queryset.prefetch_related("my_model_set")

## `nautobot.apps.tables.TagColumn`

Bases: `TemplateColumn`

Display a list of tags assigned to the object.

## `nautobot.apps.tables.ToggleColumn`

Bases: `CheckBoxColumn`

Extend CheckBoxColumn to add a "toggle all" checkbox in the column header.