Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

258
Visualizações
List of object attributes in pydantic model

I use Fast API to create a web service.

There are following sqlAlchemy models:

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 schemas are below:

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

What I have now is:

{
  "account_name": "Test.Test",
  "email": "Test.Test@test.com",
  "roles": [
    {
      "role_name": "all:admin"
    },
    {
      "role_name": "all:read"
    }
  ]
}

What I want to achieve is to get user from api in following structure:

{
  "account_name": "Test.Test",
  "email": "Test.Test@test.com",
  "roles": [
    "all:admin",
    "all:read"
  ]
}

Is that possible? How should I change schemas to get this?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

If you are okay with handling the how to "get user from api" problem statement by modifying the fastapi path definition, see below.

Can you change the response model used by the fastapi path definition in order to handle the desired output format?

Example pydantic response model definition:

class UserResponse(BaseModel):
    account_name: str
    email: EmailStr
    roles: List[str]

Example sqlalchemy query + serialization function:

def get_user_response(user_id) -> UserResponse:
    user = User.query.get(user_id)
    user_roles = UserRole.query.filter(user=user_id).all()
    role_names = [r.role_name for r in user_roles]
    response = UserResponse(
        account_name=user.account_name,
        email=user.email,
        roles=role_names
    }
    return response

Example fastapi path definition:

@app.get("/users/{user_id}", response_model=UserResponse)
async def read_item(user_id):
    return get_user_response(user_id)

Considerations:

  • I'm using a user_id for the user queries but this can be replaced with whatever you end up using as your primary key for that table.
  • The UserResponse response model is very similar to UserBase (you could potentially subclass UserBase instead of model to avoid the redefinition of account_name and email, with the tradeoff of having to override the class' Config).
  • There may be a way to override the serialization format of the UserBase sqlalchemy model object that gets automatically serialized when you query the model from the database and allows you to eliminate or reduce the code in the get_user_response() function in the example definition above.
over 4 years ago · Santiago Trujillo Relatório

0

As UserRole is a class, it is represented as an object (using a dictionary). If you want to represent it as a list of strings you'll have to transform the data (and change your Pydantic model's field declaration). There's several approaches to this, but the pydantic model documentation is a good place to start. Mind that the ORM model serves as the data representation layer and the pydantic model is the validation (and perhaps serialization) layer, so there's a lot of places where you could 'hook in'.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda