Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
marpulli committed May 3, 2023
1 parent aa0dbe3 commit e8f21c5
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 23 deletions.
4 changes: 1 addition & 3 deletions ormar/models/helpers/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,7 @@ def generate_model_example(model: Type["Model"], relation_map: Dict = None) -> D
"""
example: Dict[str, Any] = dict()
relation_map = (
relation_map
if relation_map is not None
else translate_list_to_dict(model._iterate_related_models())
relation_map if relation_map is not None else model._related_models_dict()
)
for name, field in model.Meta.model_fields.items():
populates_sample_fields_values(
Expand Down
4 changes: 1 addition & 3 deletions ormar/models/mixins/merge_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,7 @@ def merge_two_instances(
:rtype: Model
"""
relation_map = (
relation_map
if relation_map is not None
else translate_list_to_dict(one._iterate_related_models())
relation_map if relation_map is not None else one._related_models_dict()
)
for field_name in relation_map:
current_field = getattr(one, field_name)
Expand Down
11 changes: 10 additions & 1 deletion ormar/models/mixins/relation_mixin.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from typing import Callable, Dict, List, Optional, Set, TYPE_CHECKING, cast
from typing import Any, Callable, Dict, List, Optional, Set, TYPE_CHECKING, cast

from ormar import BaseField, ForeignKeyField
from ormar.models.traversible import NodeList
from ormar.queryset.utils import translate_list_to_dict


class RelationMixin:
Expand All @@ -14,6 +15,7 @@ class RelationMixin:

Meta: ModelMeta
__relation_map__: Optional[List[str]]
__relation_map_dict__: Optional[Dict[str, Any]]
_related_names: Optional[Set]
_through_names: Optional[Set]
_related_fields: Optional[List]
Expand Down Expand Up @@ -112,6 +114,13 @@ def _extract_db_related_names(cls) -> Set:
}
return related_names

@classmethod
def _related_models_dict(cls) -> dict[str, Any]:
if not cls.__relation_map_dict__:
cls.__relation_map_dict__ = translate_list_to_dict(cls._iterate_related_models())

return cls.__relation_map_dict__

@classmethod
def _iterate_related_models( # noqa: CCR001
cls,
Expand Down
4 changes: 1 addition & 3 deletions ormar/models/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,7 @@ async def save_related( # noqa: CCR001, CFQ002
:rtype: int
"""
relation_map = (
relation_map
if relation_map is not None
else translate_list_to_dict(self._iterate_related_models())
relation_map if relation_map is not None else self._related_models_dict()
)
if exclude and isinstance(exclude, Set):
exclude = translate_list_to_dict(exclude)
Expand Down
14 changes: 1 addition & 13 deletions ormar/models/newbasemodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -735,16 +735,6 @@ def _extract_nested_models( # noqa: CCR001, CFQ002
except ReferenceError:
dict_instance[field] = None
return dict_instance


@classmethod
def _related_models_dict(cls) -> dict:
if not cls.__relation_map_dict__:
cls.__relation_map_dict__ = translate_list_to_dict(
cls._iterate_related_models()
)
return cls.__relation_map_dict__


def dict( # type: ignore # noqa A003
self,
Expand Down Expand Up @@ -822,9 +812,7 @@ def dict( # type: ignore # noqa A003
exclude = translate_list_to_dict(exclude)

relation_map = (
relation_map
if relation_map is not None
else self._related_models_dict()
relation_map if relation_map is not None else self._related_models_dict()
)
pk_only = getattr(self, "__pk_only__", False)
if relation_map and not pk_only:
Expand Down

0 comments on commit e8f21c5

Please sign in to comment.