use existing encode_json to avoid code duplication, rename queryset customization param and move it to Meta, move docs to models from inheritance

This commit is contained in:
collerek
2022-01-26 17:59:00 +01:00
parent 33b492216d
commit 0e167dc89f
14 changed files with 74 additions and 112 deletions

View File

@ -12,18 +12,13 @@ from typing import (
cast,
)
try:
import orjson as json
except ImportError: # pragma: no cover
import json # type: ignore
import pydantic
import ormar # noqa: I100, I202
from ormar.exceptions import ModelPersistenceError
from ormar.fields.parsers import encode_json
from ormar.models.mixins import AliasMixin
from ormar.models.mixins.relation_mixin import RelationMixin
from ormar.queryset.utils import to_str
if TYPE_CHECKING: # pragma: no cover
from ormar import ForeignKeyField, Model
@ -208,8 +203,8 @@ class SavePrepareMixin(RelationMixin, AliasMixin):
:rtype: Dict
"""
for key, value in model_dict.items():
if key in cls._json_fields and not isinstance(value, str):
model_dict[key] = to_str(json.dumps(value))
if key in cls._json_fields:
model_dict[key] = encode_json(value)
return model_dict
@classmethod