From 3c2a55a521b1a8c8a36340bf37f036f242a514a0 Mon Sep 17 00:00:00 2001 From: Kim Johansson Date: Sun, 10 Jul 2022 11:58:45 +0200 Subject: [PATCH] Add TenantGroupColumn to display Tenant Group on tables Works the same as the existing TenantColumn, but displats the Tenant Group of the Tenant. Views should prefetch the Tenants Group for this to be efficient in large tables. --- netbox/tenancy/tables/columns.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/netbox/tenancy/tables/columns.py b/netbox/tenancy/tables/columns.py index bb5aba5de..aa66e8bc8 100644 --- a/netbox/tenancy/tables/columns.py +++ b/netbox/tenancy/tables/columns.py @@ -2,6 +2,7 @@ import django_tables2 as tables __all__ = ( 'TenantColumn', + 'TenantGroupColumn', ) @@ -24,3 +25,28 @@ class TenantColumn(tables.TemplateColumn): def value(self, value): return str(value) if value else None + + +class TenantGroupColumn(tables.TemplateColumn): + """ + Include the tenant group description. + """ + template_code = """ + {% if record.tenant and record.tenant.group %} + {{ record.tenant.group }} + {% elif record.vrf.tenant and record.vrf.tenant.group %} + {{ record.vrf.tenant.group }}* + {% else %} + — + {% endif %} + """ + + def __init__(self, *args, **kwargs): + if 'verbose_name' not in kwargs: + kwargs['verbose_name'] = 'Tenant Group' + + super().__init__(template_code=self.template_code, *args, **kwargs) + + def value(self, value): + return str(value) if value else None +