Skip to content

Commit

Permalink
improve jsonfield type hint
Browse files Browse the repository at this point in the history
  • Loading branch information
waketzheng committed Aug 25, 2024
1 parent 8108fbc commit ba92ca3
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions tortoise/fields/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -563,17 +563,20 @@ def __init__(
self.decoder = decoder

def to_db_value(
self, value: Optional[Union[dict, list, str]], instance: "Union[Type[Model], Model]"
self, value: Optional[Union[dict, list, str, bytes]], instance: "Union[Type[Model], Model]"
) -> Optional[str]:
self.validate(value)

if isinstance(value, (str, bytes)):
try:
self.decoder(value)
except Exception:
raise FieldError(f"Value {value} is invalid json value.")
return value
return None if value is None else self.encoder(value)
if value is not None:
if isinstance(value, (str, bytes)):
try:
self.decoder(value)
except Exception:
raise FieldError(f"Value {value!r} is invalid json value.")
if isinstance(value, bytes):
value = value.decode()
else:
value = self.encoder(value)
return value

def to_python_value(
self, value: Optional[Union[str, bytes, dict, list]]
Expand Down

0 comments on commit ba92ca3

Please sign in to comment.