some refactors in relations module

This commit is contained in:
collerek
2020-09-15 13:53:54 +02:00
parent df9b5d27da
commit b47d5209eb
4 changed files with 18 additions and 17 deletions

BIN
.coverage

Binary file not shown.

View File

@ -1,13 +1,17 @@
import inspect
from typing import List, Optional, Set, TYPE_CHECKING
from typing import List, Optional, Set, TYPE_CHECKING, Type, TypeVar
import ormar
from ormar.exceptions import RelationshipInstanceError
from ormar.fields import BaseField
from ormar.fields.foreign_key import ForeignKeyField
from ormar.models.metaclass import ModelMeta
if TYPE_CHECKING: # pragma no cover
from ormar import Model
Field = TypeVar("Field", bound=BaseField)
class ModelTableProxy:
if TYPE_CHECKING: # pragma no cover
@ -89,6 +93,17 @@ class ModelTableProxy:
if field.to == related.__class__ or field.to.Meta == related.Meta:
return name
@staticmethod
def resolve_relation_field(item: "Model", related: "Model") -> Type[Field]:
name = ModelTableProxy.resolve_relation_name(item, related)
to_field = item.Meta.model_fields.get(name)
if not to_field: # pragma no cover
raise RelationshipInstanceError(
f"Model {item.__class__} does not have "
f"reference to model {related.__class__}"
)
return to_field
@classmethod
def merge_instances_list(cls, result_rows: List["Model"]) -> List["Model"]:
merged_rows = []

View File

@ -39,7 +39,7 @@ class Relation:
def _find_existing(self, child: "Model") -> Optional[int]:
for ind, relation_child in enumerate(self.related_models[:]):
try:
if relation_child.__same__(child):
if relation_child == child:
return ind
except ReferenceError: # pragma no cover
self.related_models.pop(ind)

View File

@ -2,7 +2,6 @@ from typing import List, Optional, TYPE_CHECKING, Tuple, Type, Union
from weakref import proxy
import ormar
from ormar.exceptions import RelationshipInstanceError
from ormar.fields.foreign_key import ForeignKeyField
from ormar.fields.many_to_many import ManyToManyField
from ormar.relations import Relation
@ -85,20 +84,7 @@ class RelationsManager:
@staticmethod
def add(parent: "Model", child: "Model", child_name: str, virtual: bool) -> None:
to_field = next(
(
field
for field in child._orm._related_fields
if field.to == parent.__class__ or field.to.Meta == parent.Meta
),
None,
)
if not to_field: # pragma no cover
raise RelationshipInstanceError(
f"Model {child.__class__} does not have "
f"reference to model {parent.__class__}"
)
to_field = child.resolve_relation_field(child, parent)
(
parent,