Skip to content

Commit

Permalink
fix exclude_readonly param not working in pydantic_model_creator (#…
Browse files Browse the repository at this point in the history
…1594)

* fix `exclude` param not working in `pydantic_model_creator`

* Update CHANGELOG.rst

* add test case

* change test case
  • Loading branch information
Abdeldjalil-H committed Apr 25, 2024
1 parent 78ef3dd commit 5c8d81c
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Fixed
- Fix order of fields in `ValuesListQuery` when it has more than 10 fields. (#1492)
- Fix pydantic v2 pydantic_model_creator nullable field not optional. (#1454)
- Fix pydantic v2.5 unittest error. (#1535)
- Fix pydantic_model_creator `exclude_readonly` parameter not working.

0.20.0
------
Expand Down
5 changes: 5 additions & 0 deletions tests/contrib/test_pydantic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1278,6 +1278,11 @@ def test_config_classes_merge_all_configs(self):
PydanticModel.model_config["from_attributes"],
)

def test_exclude_read_only(self):
ModelPydantic = pydantic_model_creator(Event, exclude_readonly=True)

self.assertNotIn("modified", ModelPydantic.model_json_schema()["properties"])


class TestPydanticCycle(test.TestCase):
async def asyncSetUp(self) -> None:
Expand Down
2 changes: 1 addition & 1 deletion tortoise/contrib/pydantic/creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ def get_submodel(_model: "Type[Model]") -> Optional[Type[PydanticModel]]:
json_schema_extra["nullable"] = True
if fdesc.get("nullable") or field_default is not None or fname in optional:
ptype = Optional[ptype]
if not (exclude_readonly and fdesc["constraints"].get("readOnly") is True):
if not (exclude_readonly and json_schema_extra.get("readOnly") is True):
properties[fname] = annotation or ptype

if fname in properties and not isinstance(properties[fname], tuple):
Expand Down

0 comments on commit 5c8d81c

Please sign in to comment.