From a17134d5e206f50b3a0df58dd308d65e70e73e4a Mon Sep 17 00:00:00 2001 From: martsforever Date: Wed, 1 Apr 2026 22:40:00 +0800 Subject: [PATCH] feat: optimize model_utils, to_dict and to_obj --- app/utils/model_utils.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/app/utils/model_utils.py b/app/utils/model_utils.py index d015a2a..c58b849 100644 --- a/app/utils/model_utils.py +++ b/app/utils/model_utils.py @@ -136,5 +136,10 @@ def to_camel(snake_str: str) -> str: return components[0] + ''.join(x.title() for x in components[1:]) -def to_dict(cls: BaseModel): - return cls.model_dump_json(by_alias=True) +# 将SqlModel子类实例转化为字典 +def to_dict(obj: BaseModel): + return obj.model_dump_json(by_alias=True) + +# 将字典转化为SqlModel子类实例对象 +def to_obj(clazz: type[SQLModel], json_data: dict): + return clazz.model_validate(json_data)