some cleanup of unused relations code, introduced caching of related_names and props on model, set profiling

This commit is contained in:
collerek
2020-11-12 08:11:40 +01:00
parent 1242e5d600
commit e743286008
11 changed files with 95 additions and 466 deletions

View File

@ -3,7 +3,6 @@ from ormar.relations.relation import Relation, RelationType
from ormar.relations.relation_manager import RelationsManager
from ormar.relations.utils import (
get_relations_sides_and_names,
register_missing_relation,
)
__all__ = [
@ -11,6 +10,5 @@ __all__ = [
"Relation",
"RelationsManager",
"RelationType",
"register_missing_relation",
"get_relations_sides_and_names",
]

View File

@ -7,7 +7,6 @@ from ormar.fields.many_to_many import ManyToManyField
from ormar.relations.relation import Relation, RelationType
from ormar.relations.utils import (
get_relations_sides_and_names,
register_missing_relation,
)
if TYPE_CHECKING: # pragma no cover
@ -42,8 +41,6 @@ class RelationsManager:
to=field.to,
through=getattr(field, "through", None),
)
if field.name not in self._related_names:
self._related_names.append(field.name)
def __contains__(self, item: str) -> bool:
return item in self._related_names
@ -69,9 +66,10 @@ class RelationsManager:
)
parent_relation = parent._orm._get(child_name)
if not parent_relation:
parent_relation = register_missing_relation(parent, child, child_name)
parent_relation.add(child) # type: ignore
if parent_relation:
# print('missing', child_name)
# parent_relation = register_missing_relation(parent, child, child_name)
parent_relation.add(child) # type: ignore
child_relation = child._orm._get(to_name)
if child_relation:

View File

@ -72,6 +72,4 @@ class RelationProxy(list):
if self.relation._type == ormar.RelationType.MULTIPLE:
await self.queryset_proxy.create_through_instance(item)
rel_name = item.resolve_relation_name(item, self._owner)
if rel_name not in item._orm: # pragma nocover
item._orm._add_relation(item.Meta.model_fields[rel_name])
setattr(item, rel_name, self._owner)

View File

@ -1,32 +1,19 @@
from typing import Optional, TYPE_CHECKING, Tuple, Type
from typing import TYPE_CHECKING, Tuple, Type
from weakref import proxy
import ormar
from ormar.fields import BaseField
from ormar.fields.many_to_many import ManyToManyField
from ormar.relations import Relation
if TYPE_CHECKING: # pragma no cover
from ormar import Model
def register_missing_relation(
parent: "Model", child: "Model", child_name: str
) -> Optional[Relation]:
ormar.models.expand_reverse_relationships(child.__class__)
name = parent.resolve_relation_name(parent, child)
field = parent.Meta.model_fields[name]
parent._orm._add_relation(field)
parent_relation = parent._orm._get(child_name)
return parent_relation
def get_relations_sides_and_names(
to_field: Type[BaseField],
parent: "Model",
child: "Model",
child_name: str,
virtual: bool,
to_field: Type[BaseField],
parent: "Model",
child: "Model",
child_name: str,
virtual: bool,
) -> Tuple["Model", "Model", str, str]:
to_name = to_field.name
if issubclass(to_field, ManyToManyField):