Files
netbox/netbox/tenancy/models/tenants.py
Jason Novinger 0b7375136d Closes #21016: Add missing MPTT tree indexes (#21432)
Upgrade django-mptt to 0.18.0 and add empty indexes tuple to MPTT model
Meta classes. The empty tuple triggers Django's migration detection for
indexes that django-mptt adds dynamically (see
django-mptt/django-mptt#682). We cannot define the indexes explicitly
because the MPTT fields don't exist when the Meta class is evaluated.

Affected models: Region, SiteGroup, Location, DeviceRole, Platform,
ModuleBay, InventoryItem, InventoryItemTemplate, TenantGroup,
ContactGroup, WirelessLANGroup
2026-02-13 17:00:04 +01:00

94 lines
2.6 KiB
Python

from django.db import models
from django.db.models import Q
from django.utils.translation import gettext_lazy as _
from netbox.models import NestedGroupModel, PrimaryModel
from netbox.models.features import ContactsMixin
__all__ = (
'Tenant',
'TenantGroup',
)
class TenantGroup(NestedGroupModel):
"""
An arbitrary collection of Tenants.
"""
name = models.CharField(
verbose_name=_('name'),
max_length=100,
unique=True,
db_collation="natural_sort"
)
slug = models.SlugField(
verbose_name=_('slug'),
max_length=100,
unique=True
)
class Meta:
ordering = ['name']
# Empty tuple triggers Django migration detection for MPTT indexes
# (see #21016, django-mptt/django-mptt#682)
indexes = ()
verbose_name = _('tenant group')
verbose_name_plural = _('tenant groups')
class Tenant(ContactsMixin, PrimaryModel):
"""
A Tenant represents an organization served by the NetBox owner. This is typically a customer or an internal
department.
"""
name = models.CharField(
verbose_name=_('name'),
max_length=100,
db_collation="natural_sort"
)
slug = models.SlugField(
verbose_name=_('slug'),
max_length=100
)
group = models.ForeignKey(
to='tenancy.TenantGroup',
on_delete=models.SET_NULL,
related_name='tenants',
blank=True,
null=True
)
clone_fields = (
'group', 'description',
)
class Meta:
ordering = ['name']
constraints = (
models.UniqueConstraint(
fields=('group', 'name'),
name='%(app_label)s_%(class)s_unique_group_name',
violation_error_message=_("Tenant name must be unique per group.")
),
models.UniqueConstraint(
fields=('name',),
name='%(app_label)s_%(class)s_unique_name',
condition=Q(group__isnull=True)
),
models.UniqueConstraint(
fields=('group', 'slug'),
name='%(app_label)s_%(class)s_unique_group_slug',
violation_error_message=_("Tenant slug must be unique per group.")
),
models.UniqueConstraint(
fields=('slug',),
name='%(app_label)s_%(class)s_unique_slug',
condition=Q(group__isnull=True)
),
)
verbose_name = _('tenant')
verbose_name_plural = _('tenants')
def __str__(self):
return self.name