fastapi pydantic nested models
See original GitHub issueFirst check
- I added a very descriptive title to this issue.
- I used the GitHub search to find a similar issue and didn’t find it.
- I searched the FastAPI documentation, with the integrated search.
- I already searched in Google “How to X in FastAPI” and didn’t find any information.
- I already read and followed all the tutorial in the docs and didn’t find an answer.
- I already checked if it is not related to FastAPI but to Pydantic.
- I already checked if it is not related to FastAPI but to Swagger UI.
- I already checked if it is not related to FastAPI but to ReDoc.
- After submitting this, I commit to one of:
- Read open issues with questions until I find 2 issues where I can help someone and add a comment to help there.
- I already hit the “watch” button in this repository to receive notifications and I commit to help at least 2 people that ask questions in the future.
- Implement a Pull Request for a confirmed bug.
My view models below:
class EnvContainersStatusResponse(BaseModel):
description: str = Field(...)
class Config:
orm_mode = True
allow_population_by_field_name = True
class EnvContainersResponse(BaseModel):
container_name: str = Field(alias='_name')
container_status: EnvContainersStatusResponse
class Config:
orm_mode = True
allow_population_by_field_name = True
class EnvResponse(EnvBase):
env_id: str = Field(alias='environment_name')
container_collection: List[EnvContainersResponse] = Field(alias='compute_node_containers')
class Config:
orm_mode = True
allow_population_by_field_name = True
my response is like below
{
"environment_name": "somenewenv",
"compute_node_containers": [
{
"_name": "compute001",
"container_status": {
"description": "online"
}
}
]
}
question: how i can flatten my response model to have below:
want eliminate another dict and have only key value like “container_status”: “online”
{
"environment_name": "somenewenv",
"compute_node_containers": [
{
"_name": "compute001",
"container_status": "online"
}
]
}
Issue Analytics
- State:
- Created 3 years ago
- Comments:11 (1 by maintainers)
Top Results From Across the Web
Body - Nested Models - FastAPI
Each attribute of a Pydantic model has a type. But that type can itself be another Pydantic model. So, you can declare deeply...
Read more >Define a Pydantic (nested) model - python - Stack Overflow
You have a whole part explaining the usage of pydantic with fastapi here. to respond more precisely to your question pydantic models are ......
Read more >Using ormar in responses
Nested models excludes ... Despite the fact that fastapi allows passing only set of field names, so simple excludes, when using response_model_exclude ,...
Read more >tiangolo/fastapi - Gitter
I need to know how to get nested models, while performing get method from a sql query. ... import asyncio import aiosql import...
Read more >How to Make the Most of Pydantic - Towards Data Science
The structure defines a cat entry with a nested definition of an address. So then, defining a Pydantic model to tackle this could...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@aaronstephenson, I solved this by adding
response_model_by_alias
.@grillazz I actually came up today to the need to implement this functionality in a many-to-many relationship. Here is my implementation of the first proposal. I will assume that you are using SQLAlchemy for ORM, that you are traversing the entities relationships through
relationship
model attributes and that you have initialized the declarativeBase
class somewhere.This should result in what you are looking for.