优化更新
This commit is contained in:
@@ -163,27 +163,25 @@ def __init_subclass__(cls, **kwargs):
|
||||
### 序列化方法
|
||||
|
||||
```python
|
||||
# ============================================================
|
||||
# 子类钩子:自动将 __table_comment__ 写入 __table_args__
|
||||
# ============================================================
|
||||
def __init_subclass__(cls, **kwargs):
|
||||
super().__init_subclass__(**kwargs)
|
||||
comment = getattr(cls, '__table_comment__', None)
|
||||
if not comment:
|
||||
return
|
||||
# 仅对有 __tablename__ 的具体模型操作(跳过纯抽象中间层)
|
||||
if not getattr(cls, '__tablename__', None):
|
||||
return
|
||||
existing = getattr(cls, '__table_args__', {})
|
||||
if isinstance(existing, dict):
|
||||
merged = {**existing}
|
||||
elif isinstance(existing, tuple) and existing:
|
||||
merged = dict(existing[-1]) if isinstance(existing[-1], dict) else {}
|
||||
else:
|
||||
merged = {}
|
||||
if 'comment' not in merged:
|
||||
merged['comment'] = comment
|
||||
cls.__table_args__ = merged
|
||||
# src/core/base_model.py
|
||||
def to_dict(self) -> Dict[str, Any]:
|
||||
"""将模型实例转换为字典"""
|
||||
result = {}
|
||||
for column in self.__table__.columns:
|
||||
if column.name in SERIALIZE_EXCLUDE_FIELDS:
|
||||
continue
|
||||
value = getattr(self, column.name)
|
||||
if isinstance(value, datetime):
|
||||
if value.tzinfo is None:
|
||||
# MySQL DATETIME 无时区信息,读回为 naive UTC,统一按 UTC 转本地
|
||||
value = value.replace(tzinfo=timezone.utc)
|
||||
value = value.astimezone().strftime('%Y-%m-%d %H:%M:%S')
|
||||
elif isinstance(value, date):
|
||||
value = value.strftime('%Y-%m-%d')
|
||||
# 字段名转驼峰
|
||||
camel_key = convert_camel_case(column.name)
|
||||
result[camel_key] = value
|
||||
return result
|
||||
```
|
||||
|
||||
关键设计点:
|
||||
@@ -416,7 +414,7 @@ Pydantic Schema 可能用 camelCase(如 `parentId`),而模型列是 snake_
|
||||
|
||||
| 方法 | 说明 | 返回值 |
|
||||
|:---|:---|:---|
|
||||
| `get_page(request)` | 分页查询 | `R.page(records, total, size, current, pages)` |
|
||||
| `get_page(request)` | 分页查询 | `R.page(data, total, current, size)` |
|
||||
| `get_list(request)` | 不分页查询 | `R.ok(data=[...])` |
|
||||
| `get_detail(obj_id)` | 详情查询 | `dict` 或 `None` |
|
||||
| `add(request, data)` | 新增记录 | `R.ok(msg='添加成功')` |
|
||||
|
||||
Reference in New Issue
Block a user