我使用 Fast API 创建 Web 服务。
有以下 sqlAlchemy 模型:
class User(Base):
__tablename__ = 'user'
account_name = Column(String, primary_key=True, index=True, unique=True)
email = Column(String, unique=True, index=True, nullable=False)
roles = relationship("UserRole", back_populates="users", lazy=False, uselist=True)
class UserRole(Base):
__tablename__ = 'user_role'
__table_args__ = (UniqueConstraint('role_name', 'user_name', name='user_role_uc'),)
role_name = Column(String, ForeignKey('role.name'), primary_key=True)
user_name = Column(String, ForeignKey('user.account_name'), primary_key=True)
users = relationship("User", back_populates="roles")
Pydantic模式如下:
class UserRole(BaseModel):
role_name: str
class Config:
orm_mode = True
class UserBase(BaseModel):
account_name: str
email: EmailStr
roles: List[UserRole] = []
class Config:
orm_mode = True
我现在拥有的是:
{
"account_name": "Test.Test",
"email": "Test.Test@test.com",
"roles": [
{
"role_name": "all:admin"
},
{
"role_name": "all:read"
}
]
}
我想要实现的是从以下结构中的 api 获取用户:
{
"account_name": "Test.Test",
"email": "Test.Test@test.com",
"roles": [
"all:admin",
"all:read"
]
}
那可能吗?我应该如何更改架构来获得这个?