字段dict
# Assuming `session` is your SQLAlchemy session
user_instance = session.query(User).first()# Convert to Pydantic model
dapter = TypeAdapter(User, UserPydantic)
user_pydantic = adapter.to_pydantic(user_instance)# 也可以
UserPydantic.model_validate(user_instance)
print(user_pydantic.json())
列表
# Assuming `session` is your SQLAlchemy session
user_instances = session.query(User).all()# Convert to Pydantic model
TypeAdapter(List[UserPydantic]).validate_python(user_instances)
模型设置
class UserPydantic(BaseModel):id: intname: stremail: strlabel_json: List[Dict]class Config:orm_mode = Truefrom_attributes=Truealias_generator = to_camelpopulate_by_name = True
``