fix json to bytes when bulk-create
This commit is contained in:
@ -23,6 +23,7 @@ import ormar # noqa: I100, I202
|
|||||||
from ormar.exceptions import ModelPersistenceError
|
from ormar.exceptions import ModelPersistenceError
|
||||||
from ormar.models.mixins import AliasMixin
|
from ormar.models.mixins import AliasMixin
|
||||||
from ormar.models.mixins.relation_mixin import RelationMixin
|
from ormar.models.mixins.relation_mixin import RelationMixin
|
||||||
|
from ormar.queryset.utils import to_str
|
||||||
|
|
||||||
if TYPE_CHECKING: # pragma: no cover
|
if TYPE_CHECKING: # pragma: no cover
|
||||||
from ormar import ForeignKeyField, Model
|
from ormar import ForeignKeyField, Model
|
||||||
@ -208,7 +209,7 @@ class SavePrepareMixin(RelationMixin, AliasMixin):
|
|||||||
"""
|
"""
|
||||||
for key, value in model_dict.items():
|
for key, value in model_dict.items():
|
||||||
if key in cls._json_fields and not isinstance(value, str):
|
if key in cls._json_fields and not isinstance(value, str):
|
||||||
model_dict[key] = json.dumps(value)
|
model_dict[key] = to_str(json.dumps(value))
|
||||||
return model_dict
|
return model_dict
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
|
|||||||
@ -17,6 +17,13 @@ if TYPE_CHECKING: # pragma no cover
|
|||||||
from ormar import Model, BaseField
|
from ormar import Model, BaseField
|
||||||
|
|
||||||
|
|
||||||
|
def to_str(val: Union[bytes, str]):
|
||||||
|
""" convert bytes to str simply """
|
||||||
|
if isinstance(val, bytes):
|
||||||
|
return val.decode()
|
||||||
|
return str(val)
|
||||||
|
|
||||||
|
|
||||||
def check_node_not_dict_or_not_last_node(
|
def check_node_not_dict_or_not_last_node(
|
||||||
part: str, is_last: bool, current_level: Any
|
part: str, is_last: bool, current_level: Any
|
||||||
) -> bool:
|
) -> bool:
|
||||||
|
|||||||
@ -42,6 +42,7 @@ class ToDo(ormar.Model):
|
|||||||
id: int = ormar.Integer(primary_key=True)
|
id: int = ormar.Integer(primary_key=True)
|
||||||
text: str = ormar.String(max_length=500)
|
text: str = ormar.String(max_length=500)
|
||||||
completed: bool = ormar.Boolean(default=False)
|
completed: bool = ormar.Boolean(default=False)
|
||||||
|
pairs: List[str] = ormar.JSON(default=[])
|
||||||
|
|
||||||
|
|
||||||
class Category(ormar.Model):
|
class Category(ormar.Model):
|
||||||
|
|||||||
Reference in New Issue
Block a user