Skip to content

Commit

Permalink
fix: pydantic warn
Browse files Browse the repository at this point in the history
  • Loading branch information
long2ice committed Jul 21, 2023
1 parent 0667246 commit 82364d4
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions tortoise/contrib/pydantic/creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,10 @@ def field_map_update(keys: tuple, is_relation=True) -> None:
# Process fields
for fname, fdesc in field_map.items():
comment = ""
fconfig: Dict[str, Any] = {}

json_schema_extra: Dict[str, Any] = {}
fconfig: Dict[str, Any] = {
"json_schema_extra": json_schema_extra,
}
field_type = fdesc["field_type"]
field_default = fdesc.get("default")

Expand Down Expand Up @@ -354,7 +356,7 @@ def get_submodel(_model: "Type[Model]") -> Optional[Type[PydanticModel]]:
model = get_submodel(fdesc["python_type"])
if model:
if fdesc.get("nullable"):
fconfig["nullable"] = True
json_schema_extra["nullable"] = True
if fdesc.get("nullable") or field_default is not None:
model = Optional[model] # type: ignore

Expand Down Expand Up @@ -385,10 +387,13 @@ def get_submodel(_model: "Type[Model]") -> Optional[Type[PydanticModel]]:
# Any other tortoise fields
else:
annotation = annotations.get(fname, None)
if "readOnly" in fdesc["constraints"]:
json_schema_extra["readOnly"] = fdesc["constraints"]["readOnly"]
del fdesc["constraints"]["readOnly"]
fconfig.update(fdesc["constraints"])
ptype = fdesc["python_type"]
if fdesc.get("nullable"):
fconfig["nullable"] = True
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):
Expand Down Expand Up @@ -483,7 +488,7 @@ def pydantic_queryset_creator(
model = create_model(
lname,
__base__=PydanticListModel,
__annotations__={"root": List[submodel]}, # type: ignore
root=(List[submodel], Field(default_factory=list)),
)
# Copy the Model docstring over
model.__doc__ = _cleandoc(cls)
Expand Down

0 comments on commit 82364d4

Please sign in to comment.