[fix] Pydantic save json error

This commit is contained in:
Mark
2026-01-12 15:59:10 +08:00
parent 7a0746cf4e
commit f2390412d2
5 changed files with 105 additions and 22 deletions

View File

@@ -8,6 +8,7 @@ from sqlalchemy import Column, String, Boolean, DateTime, Integer, Float, Text,
from sqlalchemy.dialects.postgresql import UUID, JSON
from sqlalchemy.orm import relationship
from app.base.type import PydanticType
from app.db import Base
from app.schemas import ModelParameters
@@ -23,27 +24,27 @@ class AggregationStrategy(StrEnum):
VOTE = "vote"
PRIORITY = "priority"
class PydanticType(TypeDecorator):
impl = JSON
# class PydanticType(TypeDecorator):
# impl = JSON
def __init__(self, pydantic_model: type[BaseModel]):
super().__init__()
self.model = pydantic_model
# def __init__(self, pydantic_model: type[BaseModel]):
# super().__init__()
# self.model = pydantic_model
def process_bind_param(self, value, dialect):
# 入库Model -> dict
if value is None:
return None
if isinstance(value, self.model):
return value.dict()
return value # 已经是 dict 也放行
# def process_bind_param(self, value, dialect):
# # 入库Model -> dict
# if value is None:
# return None
# if isinstance(value, self.model):
# return value.dict()
# return value # 已经是 dict 也放行
def process_result_value(self, value, dialect):
# 出库dict -> Model
if value is None:
return None
# return self.model.parse_obj(value) # pydantic v1
return self.model.model_validate(value) # pydantic v2
# def process_result_value(self, value, dialect):
# # 出库dict -> Model
# if value is None:
# return None
# # return self.model.parse_obj(value) # pydantic v1
# return self.model.model_validate(value) # pydantic v2
class MultiAgentConfig(Base):
"""多 Agent 配置表"""