Files
fonrey/apps/org/models/org_unit.py
ishenwei 9a7d06b34e feat: scaffold Django multi-tenant project with 5 of 9 apps
Phase 1 scaffolding: config/, core/, base models, AES-256-GCM phone encryption, enums mirror

apps.tenant: Tenant + Domain (django-tenants)

apps.org: 11 models (OrgUnit hierarchy, Staff, audit logs)

apps.account: 4 models (UserAccount as AUTH_USER_MODEL, login/password tracking)

apps.permission: 7 models (RBAC + overrides + datascope + append-only changelog)

apps.region: 5 models (District, BusinessArea, MetroLine, MetroStation, School)

All migrations generated, manage.py check passes
2026-04-29 17:01:55 +08:00

58 lines
2.1 KiB
Python

from django.db import models
from core.enums import OrgUnitAttribute, OrgUnitType
from core.models.base import SoftDeleteModel
class OrgUnit(SoftDeleteModel):
name = models.CharField(max_length=100)
type = models.CharField(max_length=20, choices=OrgUnitType.choices)
parent = models.ForeignKey(
"self",
null=True,
blank=True,
on_delete=models.RESTRICT,
related_name="children",
db_index=True,
)
path = models.TextField(
help_text="Materialized path: /root_id/.../self_id/ for subtree queries.",
)
depth = models.SmallIntegerField(default=0)
sort_order = models.IntegerField(default=0)
attribute = models.CharField(
max_length=10,
choices=OrgUnitAttribute.choices,
null=True,
blank=True,
)
address_city = models.CharField(max_length=50, blank=True, default="")
address_district = models.CharField(max_length=50, blank=True, default="")
address_detail = models.CharField(max_length=200, blank=True, default="")
latitude = models.DecimalField(max_digits=10, decimal_places=7, null=True, blank=True)
longitude = models.DecimalField(max_digits=10, decimal_places=7, null=True, blank=True)
manager = models.ForeignKey(
"org.Staff",
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name="managed_org_units",
)
established_at = models.DateField(null=True, blank=True)
phone = models.CharField(max_length=30, blank=True, default="")
ext_start = models.IntegerField(null=True, blank=True)
ext_end = models.IntegerField(null=True, blank=True)
is_active = models.BooleanField(default=True)
class Meta:
db_table = "org_units"
indexes = [
models.Index(fields=["parent"], name="idx_org_units_parent"),
models.Index(fields=["type"], name="idx_org_units_type"),
models.Index(fields=["path"], name="idx_org_units_path"),
]
ordering = ["sort_order", "name"]
def __str__(self) -> str:
return f"{self.name} ({self.type})"