feat: 抽离 BasicModel
This commit is contained in:
@@ -0,0 +1,26 @@
|
|||||||
|
from datetime import datetime, date
|
||||||
|
from datetime import datetime, date
|
||||||
|
|
||||||
|
from pydantic import ConfigDict
|
||||||
|
from sqlmodel import SQLModel, Field
|
||||||
|
|
||||||
|
from app.utils.model_utils import to_camel, format_datetime_to_string, format_date_to_string, FormattedDatetime, current_datetime
|
||||||
|
|
||||||
|
|
||||||
|
class BasicModel(SQLModel):
|
||||||
|
# Pydantic V2 的模型配置
|
||||||
|
model_config = ConfigDict(
|
||||||
|
alias_generator=to_camel, # 使用 to_camel 函数生成别名
|
||||||
|
populate_by_name=True, # 允许通过原始字段名(snake_case)赋值
|
||||||
|
extra='ignore', # 忽略模型中未定义的额外字段,避免验证失败
|
||||||
|
json_encoders={
|
||||||
|
datetime: format_datetime_to_string, # 为 datetime 类型指定自定义的 JSON 编码器
|
||||||
|
date: format_date_to_string # 为 date 类型指定自定义的 JSON 编码器
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
id: str | None = Field(default=None, primary_key=True, description="唯一标识,编号")
|
||||||
|
created_at: FormattedDatetime | None = Field(default_factory=current_datetime, description="创建时间")
|
||||||
|
updated_at: FormattedDatetime | None = Field(default_factory=current_datetime, description="更新时间")
|
||||||
|
created_by: str | None = Field(default=None, description="创建人id")
|
||||||
|
updated_by: str | None = Field(default=None, description="更新人id")
|
||||||
@@ -1,36 +1,19 @@
|
|||||||
import uuid
|
import uuid
|
||||||
from datetime import datetime, timezone, timedelta, date
|
|
||||||
from decimal import Decimal
|
from decimal import Decimal
|
||||||
from typing import List
|
from typing import List
|
||||||
|
|
||||||
from fastapi import FastAPI, APIRouter, HTTPException
|
from fastapi import FastAPI, APIRouter, HTTPException
|
||||||
from pydantic import BaseModel, ConfigDict
|
from pydantic import BaseModel
|
||||||
from sqlmodel import SQLModel, Field, select
|
from sqlmodel import Field, select
|
||||||
|
|
||||||
from app.utils.model_utils import to_camel, format_datetime_to_string, format_date_to_string, FormattedDatetime, FormattedDate, current_datetime, FormattedDecimal
|
from app.model.BasicModel import BasicModel
|
||||||
|
from app.utils.model_utils import FormattedDatetime, FormattedDate, FormattedDecimal
|
||||||
from app.utils.mysql_utils import AsyncSessionDep
|
from app.utils.mysql_utils import AsyncSessionDep
|
||||||
|
|
||||||
|
|
||||||
class LlmDemoModel(SQLModel, table=True):
|
class LlmDemoModel(BasicModel, table=True):
|
||||||
# Pydantic V2 的模型配置
|
|
||||||
model_config = ConfigDict(
|
|
||||||
alias_generator=to_camel, # 使用 to_camel 函数生成别名
|
|
||||||
populate_by_name=True, # 允许通过原始字段名(snake_case)赋值
|
|
||||||
extra='ignore', # 忽略模型中未定义的额外字段,避免验证失败
|
|
||||||
json_encoders={
|
|
||||||
datetime: format_datetime_to_string, # 为 datetime 类型指定自定义的 JSON 编码器
|
|
||||||
date: format_date_to_string # 为 date 类型指定自定义的 JSON 编码器
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
__tablename__ = "llm_demo"
|
__tablename__ = "llm_demo"
|
||||||
|
|
||||||
id: str | None = Field(default=None, primary_key=True, description="唯一标识,编号")
|
|
||||||
created_at: FormattedDatetime | None = Field(default_factory=current_datetime, description="创建时间")
|
|
||||||
updated_at: FormattedDatetime | None = Field(default_factory=current_datetime, description="更新时间")
|
|
||||||
created_by: str | None = Field(default=None, description="创建人id")
|
|
||||||
updated_by: str | None = Field(default=None, description="更新人id")
|
|
||||||
|
|
||||||
full_name: str | None = Field(default=None, description="用户名称")
|
full_name: str | None = Field(default=None, description="用户名称")
|
||||||
datetime_start: FormattedDatetime | None = Field(default=None, description="开通会员时间")
|
datetime_start: FormattedDatetime | None = Field(default=None, description="开通会员时间")
|
||||||
datetime_end: FormattedDatetime | None = Field(default=None, description="会员截止到期时间")
|
datetime_end: FormattedDatetime | None = Field(default=None, description="会员截止到期时间")
|
||||||
|
|||||||
Reference in New Issue
Block a user