some refactors in relations module
This commit is contained in:
@ -1,13 +1,17 @@
|
|||||||
import inspect
|
import inspect
|
||||||
from typing import List, Optional, Set, TYPE_CHECKING
|
from typing import List, Optional, Set, TYPE_CHECKING, Type, TypeVar
|
||||||
|
|
||||||
import ormar
|
import ormar
|
||||||
|
from ormar.exceptions import RelationshipInstanceError
|
||||||
|
from ormar.fields import BaseField
|
||||||
from ormar.fields.foreign_key import ForeignKeyField
|
from ormar.fields.foreign_key import ForeignKeyField
|
||||||
from ormar.models.metaclass import ModelMeta
|
from ormar.models.metaclass import ModelMeta
|
||||||
|
|
||||||
if TYPE_CHECKING: # pragma no cover
|
if TYPE_CHECKING: # pragma no cover
|
||||||
from ormar import Model
|
from ormar import Model
|
||||||
|
|
||||||
|
Field = TypeVar("Field", bound=BaseField)
|
||||||
|
|
||||||
|
|
||||||
class ModelTableProxy:
|
class ModelTableProxy:
|
||||||
if TYPE_CHECKING: # pragma no cover
|
if TYPE_CHECKING: # pragma no cover
|
||||||
@ -89,6 +93,17 @@ class ModelTableProxy:
|
|||||||
if field.to == related.__class__ or field.to.Meta == related.Meta:
|
if field.to == related.__class__ or field.to.Meta == related.Meta:
|
||||||
return name
|
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
|
@classmethod
|
||||||
def merge_instances_list(cls, result_rows: List["Model"]) -> List["Model"]:
|
def merge_instances_list(cls, result_rows: List["Model"]) -> List["Model"]:
|
||||||
merged_rows = []
|
merged_rows = []
|
||||||
|
|||||||
@ -39,7 +39,7 @@ class Relation:
|
|||||||
def _find_existing(self, child: "Model") -> Optional[int]:
|
def _find_existing(self, child: "Model") -> Optional[int]:
|
||||||
for ind, relation_child in enumerate(self.related_models[:]):
|
for ind, relation_child in enumerate(self.related_models[:]):
|
||||||
try:
|
try:
|
||||||
if relation_child.__same__(child):
|
if relation_child == child:
|
||||||
return ind
|
return ind
|
||||||
except ReferenceError: # pragma no cover
|
except ReferenceError: # pragma no cover
|
||||||
self.related_models.pop(ind)
|
self.related_models.pop(ind)
|
||||||
|
|||||||
@ -2,7 +2,6 @@ from typing import List, Optional, TYPE_CHECKING, Tuple, Type, Union
|
|||||||
from weakref import proxy
|
from weakref import proxy
|
||||||
|
|
||||||
import ormar
|
import ormar
|
||||||
from ormar.exceptions import RelationshipInstanceError
|
|
||||||
from ormar.fields.foreign_key import ForeignKeyField
|
from ormar.fields.foreign_key import ForeignKeyField
|
||||||
from ormar.fields.many_to_many import ManyToManyField
|
from ormar.fields.many_to_many import ManyToManyField
|
||||||
from ormar.relations import Relation
|
from ormar.relations import Relation
|
||||||
@ -85,20 +84,7 @@ class RelationsManager:
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def add(parent: "Model", child: "Model", child_name: str, virtual: bool) -> None:
|
def add(parent: "Model", child: "Model", child_name: str, virtual: bool) -> None:
|
||||||
to_field = next(
|
to_field = child.resolve_relation_field(child, parent)
|
||||||
(
|
|
||||||
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__}"
|
|
||||||
)
|
|
||||||
|
|
||||||
(
|
(
|
||||||
parent,
|
parent,
|
||||||
|
|||||||
Reference in New Issue
Block a user