intorduce relation flags on basefield and simplify imports

This commit is contained in:
collerek
2021-02-21 17:46:06 +01:00
parent c560245760
commit e697235172
27 changed files with 163 additions and 149 deletions

View File

@ -1,17 +1,13 @@
from enum import Enum
from typing import List, Optional, Set, TYPE_CHECKING, Type, TypeVar, Union
from typing import List, Optional, Set, TYPE_CHECKING, Type, Union
import ormar # noqa I100
from ormar.exceptions import RelationshipInstanceError # noqa I100
from ormar.fields.foreign_key import ForeignKeyField # noqa I100
from ormar.relations.relation_proxy import RelationProxy
if TYPE_CHECKING: # pragma no cover
from ormar import Model
from ormar.relations import RelationsManager
from ormar.models import NewBaseModel
T = TypeVar("T", bound=Model)
from ormar.models import Model, NewBaseModel, T
class RelationType(Enum):
@ -39,7 +35,7 @@ class Relation:
manager: "RelationsManager",
type_: RelationType,
field_name: str,
to: Type["T"],
to: Type["Model"],
through: Type["T"] = None,
) -> None:
"""
@ -63,10 +59,10 @@ class Relation:
self._owner: "Model" = manager.owner
self._type: RelationType = type_
self._to_remove: Set = set()
self.to: Type["T"] = to
self._through: Optional[Type["T"]] = through
self.to: Type["Model"] = to
self._through = through
self.field_name: str = field_name
self.related_models: Optional[Union[RelationProxy, "T"]] = (
self.related_models: Optional[Union[RelationProxy, "Model"]] = (
RelationProxy(relation=self, type_=type_, field_name=field_name)
if type_ in (RelationType.REVERSE, RelationType.MULTIPLE)
else None
@ -161,7 +157,7 @@ class Relation:
self.related_models.pop(position) # type: ignore
del self._owner.__dict__[relation_name][position]
def get(self) -> Optional[Union[List["T"], "T"]]:
def get(self) -> Optional[Union[List["Model"], "Model"]]:
"""
Return the related model or models from RelationProxy.