Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache the relation map dictionary #1076

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ormar/models/helpers/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,11 @@ def populate_default_options_values( # noqa: CCR001
}

new_model.__relation_map__ = None
new_model.__relation_map_dict__ = None
new_model.__ormar_fields_validators__ = None



class Connection(sqlite3.Connection):
def __init__(self, *args: Any, **kwargs: Any) -> None: # pragma: no cover
super().__init__(*args, **kwargs)
Expand Down
4 changes: 1 addition & 3 deletions ormar/models/helpers/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ def generate_model_example(
"""
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.ormar_config.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
12 changes: 11 additions & 1 deletion ormar/models/mixins/relation_mixin.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from typing import TYPE_CHECKING, Callable, Dict, List, Optional, Set, cast
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Set, 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 +16,7 @@ class RelationMixin:

ormar_config: OrmarConfig
__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 @@ -116,6 +119,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 @@ -156,9 +156,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
5 changes: 2 additions & 3 deletions ormar/models/newbasemodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class NewBaseModel(pydantic.BaseModel, ModelTableProxy, metaclass=ModelMetaclass
if TYPE_CHECKING: # pragma no cover
pk: Any
__relation_map__: Optional[List[str]]
__relation_map_dict__: Optional[dict[str, Any]]
__cached_hash__: Optional[int]
_orm_relationship_manager: AliasManager
_orm: RelationsManager
Expand Down Expand Up @@ -881,9 +882,7 @@ def model_dump( # type: ignore # noqa A003
)

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()
)
pk_only = getattr(self, "__pk_only__", False)
if relation_map and not pk_only:
Expand Down
Loading