Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

257
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!