Nested model not included in Pydantic model
See original GitHub issueIssue Description
Describe the bug I don’t know if it comes from FastAPI, Pydantic or Ormar. When retrieving a Pydantic model from an Ormar model, the response example shown in the Swagger interface does not include a nested model. Same with the actual responses: they do not contain that nested model.
My models:
# meta classes removed for conciseness
class Library(ormar.Model):
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=100)
class Package(ormar.Model):
id: int = ormar.Integer(primary_key=True)
library: Library = ormar.ForeignKey(Library, related_name="packages") # <-- of interest
version: str = ormar.String(max_length=100)
class Ticket(ormar.Model):
id: int = ormar.Integer(primary_key=True)
number: int = ormar.Integer()
status: str = ormar.String(max_length=100)
class TicketPackage(ormar.Model):
id: int = ormar.Integer(primary_key=True)
status: str = ormar.String(max_length=100)
ticket: Ticket = ormar.ForeignKey(Ticket, related_name="packages")
package: Package = ormar.ForeignKey(Package, related_name="tickets") # <-- of interest
My route:
router = APIRouter(prefix="/tickets")
TicketPackageOut = TicketPackage.get_pydantic(exclude={"ticket"})
@router.get(
"/{ticket_id}/packages",
response_model=List[TicketPackageOut],
)
async def get_ticket_packages(ticket_id: int) -> List[TicketPackage]:
return await TicketPackage.objects.select_related("package__library").filter(ticket__id=ticket_id).all()
# Note how I "select the related package library".
# If I print the return value of that query,
# I can see that the library objects are here
# with all their values (name) populated, as expected.
Expected behavior
The example and actual responses in Swagger should look like:
[
{
"id": 0,
"status": "string",
"package": {
"version": "string",
"id": 0,
"library": {
"id": 0,
"name": "string"
}
}
}
]
Actual behavior
The responses look like:
[
{
"id": 0,
"status": "string",
"package": {
"version": "string",
"id": 0
}
}
]
The package library is missing!
Screenshots
Example response | Actual response |
---|---|
![]() |
![]() |
You’ll notice extra fields that I removed from the code above for conciseness.
Versions:
- Database backend used: sqlite
- Python version: 3.8.11
ormar
version: 0.10.14pydantic
version: 1.8.2- if applicable
fastapi
version: 0.65.2
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (7 by maintainers)
Top Results From Across the Web
Define a Pydantic (nested) model - python - Stack Overflow
I would like to create a Pydantic model for managing this data structure (I mean to formally define these objects).
Read more >Body - Nested Models - FastAPI
Body - Nested Models¶. With FastAPI, you can define, validate, document, and use arbitrarily deeply nested models (thanks to Pydantic).
Read more >Models - pydantic
User here is a model with two fields id which is an integer and is required, and name which is a string and...
Read more >Serialization — Piccolo 0.101.0 documentation - Read the Docs
Another great feature is nested=True . For each ForeignKey in the Piccolo Table , the Pydantic model will contain a sub model for...
Read more >Settings management - pydantic
If you create a model that inherits from BaseSettings , the model initialiser will attempt to determine the values of any fields not...
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
Well this diff seems to fix the issue!
Fixed by #280