- core: config、security、exceptions、responses、error_codes、db、main - models: base、business(17个模型类)、system(6个模型类)、__init__ - 每个函数和类均标注作用、返回值、被调用方 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
29 lines
840 B
Python
29 lines
840 B
Python
"""ORM 模型公共 Mixin 定义。
|
||
|
||
提供时间戳和软删除等通用字段,被所有业务模型继承。
|
||
"""
|
||
|
||
from sqlalchemy import DateTime, func
|
||
from sqlalchemy.orm import Mapped, mapped_column
|
||
|
||
|
||
class TimestampMixin:
|
||
"""时间戳 Mixin,自动维护 created_at 和 updated_at 字段。
|
||
|
||
created_at 由数据库服务器默认值设置,updated_at 在每次更新时自动刷新。
|
||
"""
|
||
created_at: Mapped[DateTime] = mapped_column(DateTime, server_default=func.now())
|
||
updated_at: Mapped[DateTime] = mapped_column(
|
||
DateTime,
|
||
server_default=func.now(),
|
||
onupdate=func.now(),
|
||
)
|
||
|
||
|
||
class AuditMixin:
|
||
"""软删除 Mixin,deleted=0 表示正常,deleted=1 表示已删除。
|
||
|
||
查询时需手动过滤 deleted == 0。
|
||
"""
|
||
deleted: Mapped[int] = mapped_column(default=0)
|