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

@ -21,7 +21,7 @@ def is_field_an_forward_ref(field: Type["BaseField"]) -> bool:
:return: result of the check
:rtype: bool
"""
return issubclass(field, ormar.ForeignKeyField) and (
return field.is_relation and (
field.to.__class__ == ForwardRef or field.through.__class__ == ForwardRef
)

View File

@ -6,14 +6,15 @@ from pydantic.fields import ModelField
from pydantic.utils import lenient_issubclass
import ormar # noqa: I100, I202
from ormar.fields import BaseField, ManyToManyField
from ormar.fields import BaseField
if TYPE_CHECKING: # pragma no cover
from ormar import Model
from ormar.fields import ManyToManyField
def create_pydantic_field(
field_name: str, model: Type["Model"], model_field: Type[ManyToManyField]
field_name: str, model: Type["Model"], model_field: Type["ManyToManyField"]
) -> None:
"""
Registers pydantic field on through model that leads to passed model
@ -59,7 +60,7 @@ def get_pydantic_field(field_name: str, model: Type["Model"]) -> "ModelField":
def populate_default_pydantic_field_value(
ormar_field: Type[BaseField], field_name: str, attrs: dict
ormar_field: Type["BaseField"], field_name: str, attrs: dict
) -> dict:
"""
Grabs current value of the ormar Field in class namespace

View File

@ -25,7 +25,7 @@ def validate_related_names_in_relations( # noqa CCR001
"""
already_registered: Dict[str, List[Optional[str]]] = dict()
for field in model_fields.values():
if issubclass(field, ormar.ForeignKeyField):
if field.is_relation:
to_name = (
field.to.get_name()
if not field.to.__class__ == ForwardRef

View File

@ -1,14 +1,14 @@
from typing import TYPE_CHECKING, Type
from typing import TYPE_CHECKING, Type, cast
import ormar
from ormar import ForeignKey, ManyToMany
from ormar.fields import ManyToManyField, Through, ThroughField
from ormar.fields.foreign_key import ForeignKeyField
from ormar.fields import Through
from ormar.models.helpers.sqlalchemy import adjust_through_many_to_many_model
from ormar.relations import AliasManager
if TYPE_CHECKING: # pragma no cover
from ormar import Model
from ormar.fields import ManyToManyField, ForeignKeyField
alias_manager = AliasManager()
@ -32,7 +32,7 @@ def register_relation_on_build(field: Type["ForeignKeyField"]) -> None:
)
def register_many_to_many_relation_on_build(field: Type[ManyToManyField]) -> None:
def register_many_to_many_relation_on_build(field: Type["ManyToManyField"]) -> None:
"""
Registers connection between through model and both sides of the m2m relation.
Registration include also reverse relation side to be able to join both sides.
@ -83,10 +83,8 @@ def expand_reverse_relationships(model: Type["Model"]) -> None:
"""
model_fields = list(model.Meta.model_fields.values())
for model_field in model_fields:
if (
issubclass(model_field, ForeignKeyField)
and not model_field.has_unresolved_forward_refs()
):
if model_field.is_relation and not model_field.has_unresolved_forward_refs():
model_field = cast(Type["ForeignKeyField"], model_field)
expand_reverse_relationship(model_field=model_field)
@ -102,7 +100,7 @@ def register_reverse_model_fields(model_field: Type["ForeignKeyField"]) -> None:
:type model_field: relation Field
"""
related_name = model_field.get_related_name()
if issubclass(model_field, ManyToManyField):
if model_field.is_multi:
model_field.to.Meta.model_fields[related_name] = ManyToMany(
model_field.owner,
through=model_field.through,
@ -114,6 +112,7 @@ def register_reverse_model_fields(model_field: Type["ForeignKeyField"]) -> None:
self_reference_primary=model_field.self_reference_primary,
)
# register foreign keys on through model
model_field = cast(Type["ManyToManyField"], model_field)
register_through_shortcut_fields(model_field=model_field)
adjust_through_many_to_many_model(model_field=model_field)
else:
@ -155,7 +154,7 @@ def register_through_shortcut_fields(model_field: Type["ManyToManyField"]) -> No
)
def register_relation_in_alias_manager(field: Type[ForeignKeyField]) -> None:
def register_relation_in_alias_manager(field: Type["ForeignKeyField"]) -> None:
"""
Registers the relation (and reverse relation) in alias manager.
The m2m relations require registration of through model between
@ -168,11 +167,12 @@ def register_relation_in_alias_manager(field: Type[ForeignKeyField]) -> None:
:param field: relation field
:type field: ForeignKey or ManyToManyField class
"""
if issubclass(field, ManyToManyField):
if field.is_multi:
if field.has_unresolved_forward_refs():
return
field = cast(Type["ManyToManyField"], field)
register_many_to_many_relation_on_build(field=field)
elif issubclass(field, ForeignKeyField) and not issubclass(field, ThroughField):
elif field.is_relation and not field.is_through:
if field.has_unresolved_forward_refs():
return
register_relation_on_build(field=field)

View File

@ -156,11 +156,7 @@ def sqlalchemy_columns_from_model_fields(
field.owner = new_model
if field.primary_key:
pkname = check_pk_column_validity(field_name, field, pkname)
if (
not field.pydantic_only
and not field.virtual
and not issubclass(field, ormar.ManyToManyField)
):
if not field.pydantic_only and not field.virtual and not field.is_multi:
columns.append(field.get_column(field.get_alias()))
return pkname, columns