61 lines
1.4 KiB
Python
61 lines
1.4 KiB
Python
"""项目分类模型
|
|
|
|
支持树形分类结构
|
|
"""
|
|
|
|
from typing import Optional, List
|
|
|
|
from sqlalchemy import BigInteger, String, Integer, Boolean, ForeignKey
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
|
|
from app.models.base import BaseModel
|
|
|
|
|
|
class Category(BaseModel):
|
|
"""项目分类表"""
|
|
|
|
__tablename__ = "categories"
|
|
|
|
category_name: Mapped[str] = mapped_column(
|
|
String(50),
|
|
nullable=False,
|
|
comment="分类名称"
|
|
)
|
|
parent_id: Mapped[Optional[int]] = mapped_column(
|
|
BigInteger,
|
|
ForeignKey("categories.id"),
|
|
nullable=True,
|
|
comment="父分类ID"
|
|
)
|
|
sort_order: Mapped[int] = mapped_column(
|
|
Integer,
|
|
nullable=False,
|
|
default=0,
|
|
comment="排序"
|
|
)
|
|
is_active: Mapped[bool] = mapped_column(
|
|
Boolean,
|
|
nullable=False,
|
|
default=True,
|
|
comment="是否启用"
|
|
)
|
|
|
|
# 关系
|
|
parent: Mapped[Optional["Category"]] = relationship(
|
|
"Category",
|
|
remote_side="Category.id",
|
|
back_populates="children"
|
|
)
|
|
children: Mapped[List["Category"]] = relationship(
|
|
"Category",
|
|
back_populates="parent"
|
|
)
|
|
projects: Mapped[List["Project"]] = relationship(
|
|
"Project",
|
|
back_populates="category"
|
|
)
|
|
|
|
|
|
# 避免循环导入
|
|
from app.models.project import Project
|