Replies: 6 comments
-
As far as I understand, FastAPI doesn't let us to mix import dataclasses
from pydantic.dataclasses import dataclass
@dataclass # or @dataclasses.dataclass
class Item:
name: str
@dataclass # or @dataclasses.dataclass
class Box:
item: Item
@app.get("/item", response_model=Item)
async def item() -> Item:
return Item(name="test")
@app.get("/box", response_model=Box)
async def box() -> Box:
return Box(item=await item()) |
Beta Was this translation helpful? Give feedback.
-
This isn't what
ref: https://fastapi.tiangolo.com/advanced/dataclasses/?h=dataclasses#learn-more
Also creating an instance of >>> Box.validate(Box(item=Item(name="test")))
Box(item=Item(name="test"))) What makes you think mixing the two is not supported? |
Beta Was this translation helpful? Give feedback.
-
Neither pydantic nor FastAPI documentation made me think that mixing different types of models is not supported. I have only tried to mix I have checked the documentation but I couldn't see anything that says you could mix a Lines 75 to 80 in 672c55a Since If you call as I showed you above your response will be a Lines 101 to 102 in 672c55a Eventually, this will also be a I might be wrong. I hope there will be someone else that can have a better explanation. |
Beta Was this translation helpful? Give feedback.
-
Thanks for the pointers in the codebase! 👍 I am not sure this is the whole story, but it is definitely a step forward.
Well, there is the part that I posted in my first response that says:
With a link to that sentence. For the record, editing a post does not trigger any notification. |
Beta Was this translation helpful? Give feedback.
-
Yes, I did. Like I explained above, those were my findings and I believe |
Beta Was this translation helpful? Give feedback.
-
this is a bug pydantic/pydantic#3764 |
Beta Was this translation helpful? Give feedback.
-
First Check
Commit to Help
Example Code
Description
The
item
endpoint works fine, but thebox
one fails with aValidationError
:Operating System
Linux
Operating System Details
No response
FastAPI Version
0.71.0
Python Version
3.10.1
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions