Fix recursive ORM parsing error#2718
Fix recursive ORM parsing error#2718PrettyWood merged 1 commit intopydantic:masterfrom nuno-andre:patch-1
Conversation
There was a problem hiding this comment.
@nuno-andre Please update with tests and change file thanks
|
@PrettyWood Should I create a new test case or would you prefer I adapt |
The _ORM mode_ doesn't parse dictionaries if it's not explicitly called.
|
Thank you! |
|
This change actually prevents models (that are ORM-enabled) from being initialized the normal way from dictionaries. Prior to this change, the following code worked: from types import SimpleNamespace
from pydantic import BaseModel
class User(BaseModel):
first_name: str
last_name: str
class Config:
orm_mode = True
class State(BaseModel):
user: User
class Config: # (The issue happens regardless of whether the outer class has orm_mode set)
orm_mode = True
# Pass an "orm instance"
state = State.from_orm(SimpleNamespace(user=SimpleNamespace(first_name='John', last_name='Appleseed')))
# Pass dictionary data directly
state = State(**{'user': {'first_name': 'John', 'last_name': 'Appleseed'}})After this change, the final line throws a validation error: This is actually a bit of an issue given the way FastAPI handles response models. During the processing of a request, it serializes the data, and then passes it through a response model (if one was provided) to make sure the response matches the desired output schema. If you're using an ORM-mode model for convenience (like one generated from Tortoise-ORM's Edit: I've opened an issue about this for the sake of organization - #3181 |
The _ORM mode_ doesn't parse dictionaries if it's not explicitly called.
The ORM mode doesn't parse dictionaries if it's not explicitly called.
Change Summary
Prepend the
orm_modechecking to thedict's.Related issue number
Checklist
changes/<pull request or issue id>-<github username>.mdfile added describing change(see changes/README.md for details)
Example