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:
@ -1,12 +1,7 @@
|
||||
import base64
|
||||
from typing import Any, TYPE_CHECKING, Type
|
||||
|
||||
from ormar.queryset.utils import to_str
|
||||
|
||||
try:
|
||||
import orjson as json
|
||||
except ImportError: # pragma: no cover
|
||||
import json # type: ignore
|
||||
from ormar.fields.parsers import encode_json
|
||||
|
||||
if TYPE_CHECKING: # pragma: no cover
|
||||
from ormar import Model
|
||||
@ -42,9 +37,7 @@ class JsonDescriptor:
|
||||
return value
|
||||
|
||||
def __set__(self, instance: "Model", value: Any) -> None:
|
||||
if not isinstance(value, str):
|
||||
value = json.dumps(value)
|
||||
value = to_str(value)
|
||||
value = encode_json(value)
|
||||
instance._internal_set(self.name, value)
|
||||
instance.set_save_status(False)
|
||||
|
||||
|
||||
@ -47,18 +47,18 @@ def populate_default_options_values( # noqa: CCR001
|
||||
:param model_fields: dict of model fields
|
||||
:type model_fields: Union[Dict[str, type], Dict]
|
||||
"""
|
||||
if not hasattr(new_model.Meta, "constraints"):
|
||||
new_model.Meta.constraints = []
|
||||
if not hasattr(new_model.Meta, "model_fields"):
|
||||
new_model.Meta.model_fields = model_fields
|
||||
if not hasattr(new_model.Meta, "abstract"):
|
||||
new_model.Meta.abstract = False
|
||||
if not hasattr(new_model.Meta, "extra"):
|
||||
new_model.Meta.extra = Extra.forbid
|
||||
if not hasattr(new_model.Meta, "orders_by"):
|
||||
new_model.Meta.orders_by = []
|
||||
if not hasattr(new_model.Meta, "exclude_parent_fields"):
|
||||
new_model.Meta.exclude_parent_fields = []
|
||||
defaults = {
|
||||
"queryset_class": ormar.QuerySet,
|
||||
"constraints": [],
|
||||
"model_fields": model_fields,
|
||||
"abstract": False,
|
||||
"extra": Extra.forbid,
|
||||
"orders_by": [],
|
||||
"exclude_parent_fields": [],
|
||||
}
|
||||
for key, value in defaults.items():
|
||||
if not hasattr(new_model.Meta, key):
|
||||
setattr(new_model.Meta, key, value)
|
||||
|
||||
if any(
|
||||
is_field_an_forward_ref(field) for field in new_model.Meta.model_fields.values()
|
||||
|
||||
@ -85,6 +85,7 @@ class ModelMeta:
|
||||
orders_by: List[str]
|
||||
exclude_parent_fields: List[str]
|
||||
extra: Extra
|
||||
queryset_class: Type[QuerySet]
|
||||
|
||||
|
||||
def add_cached_properties(new_model: Type["Model"]) -> None:
|
||||
@ -614,8 +615,6 @@ class ModelMetaclass(pydantic.main.ModelMetaclass):
|
||||
|
||||
return new_model
|
||||
|
||||
__queryset_cls__ = QuerySet
|
||||
|
||||
@property
|
||||
def objects(cls: Type["T"]) -> "QuerySet[T]": # type: ignore
|
||||
if cls.Meta.requires_ref_update:
|
||||
@ -624,7 +623,7 @@ class ModelMetaclass(pydantic.main.ModelMetaclass):
|
||||
f"ForwardRefs. \nBefore using the model you "
|
||||
f"need to call update_forward_refs()."
|
||||
)
|
||||
return cls.__queryset_cls__(model_cls=cls)
|
||||
return cls.Meta.queryset_class(model_cls=cls)
|
||||
|
||||
def __getattr__(self, item: str) -> Any:
|
||||
"""
|
||||
|
||||
@ -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
|
||||
|
||||
Reference in New Issue
Block a user