Skip to content

Commit

Permalink
fixed from_orm compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalik committed Nov 17, 2023
1 parent da7b302 commit b1a492a
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 4 deletions.
2 changes: 1 addition & 1 deletion ninja/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Django Ninja - Fast Django REST framework"""

__version__ = "1.0.0"
__version__ = "1.0.1"


from pydantic import Field
Expand Down
6 changes: 3 additions & 3 deletions ninja/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ class Config:
def _run_root_validator(
cls, values: Any, handler: ModelWrapValidatorHandler[S], info: ValidationInfo
) -> Any:
# when extra is "forbid" we need to perform default pydantic valudation
# when extra is "forbid" we need to perform default pydantic validation
# as DjangoGetter does not act as dict and pydantic will not be able to validate it
if cls.model_config.get("extra") == "forbid":
handler(values)
Expand All @@ -210,8 +210,8 @@ def _run_root_validator(
return handler(values)

@classmethod
def from_orm(cls: Type[S], obj: Any) -> S:
return cls.model_validate(obj)
def from_orm(cls: Type[S], obj: Any, **kw: Any) -> S:
return cls.model_validate(obj, **kw)

def dict(self, *a: Any, **kw: Any) -> DictStrAny:
"Backward compatibility with pydantic 1.x"
Expand Down
3 changes: 3 additions & 0 deletions tests/test_schema_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ def test_schema_with_context():
obj = ResolveWithContext.model_validate({"value": 2}, context={"extra": 2})
assert obj.value == 4

obj = ResolveWithContext.from_orm({"value": 2}, context={"extra": 2})
assert obj.value == 4


def test_request_context():
resp = client.post("/resolve_ctx", json={})
Expand Down

0 comments on commit b1a492a

Please sign in to comment.