feat: optimize model service
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import asyncio
|
||||
import datetime
|
||||
import json
|
||||
from typing import Type, List, Any, Union
|
||||
from typing import Type, List, Any, Union, Optional
|
||||
|
||||
from fastapi import FastAPI, APIRouter, HTTPException, Body
|
||||
from pydantic import create_model, BaseModel
|
||||
@@ -101,7 +101,7 @@ def create_model_service(
|
||||
# 动态创建分页查询的响应模型:包含数据列表和是否有下一页的标识
|
||||
ListResponse = create_model(f"{Cls.__name__}ListResponse", list=(List[Cls], ...), has_next=(bool, ...), total=(Union[int, None], None))
|
||||
# 动态创建单条查询的响应模型:包含单个模型实例
|
||||
ItemResponse = create_model(f"{Cls.__name__}ItemResponse", result=(Cls, ...))
|
||||
ItemResponse = create_model(f"{Cls.__name__}ItemResponse", result=(Optional[Cls], ...))
|
||||
# 动态创建批量操作的响应模型:包含操作后的模型实例列表
|
||||
BatchResponse = create_model(f"{Cls.__name__}BatchResponse", result=(List[Cls], ...))
|
||||
# 动态创建批量删除的响应模型:包含删除操作是否成功的标识
|
||||
@@ -297,7 +297,7 @@ def create_model_service(
|
||||
# 返回第一条匹配的记录(若存在)
|
||||
item_cls = result.scalars().first()
|
||||
|
||||
if after_query_item is not None:
|
||||
if after_query_item is not None and item_cls is not None:
|
||||
await after_query_item(item_cls, row_dict, session)
|
||||
|
||||
return item_cls
|
||||
|
||||
Reference in New Issue
Block a user