字段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: int
name: str
email: str
label_json: List[Dict]
class Config:
orm_mode = True
from_attributes=True
alias_generator = to_camel
populate_by_name = True
``
标签:UserPydantic,sqlalchemy,模型,instance,session,user,pydantic,User
From: https://www.cnblogs.com/guanchaoguo/p/18406484