fix bug with infinite relation auto extraction, finish initial relations docs

This commit is contained in:
collerek
2020-08-14 14:35:57 +02:00
parent 002f27f21e
commit c6b4f69c4d
5 changed files with 61 additions and 49 deletions

View File

@ -35,7 +35,7 @@ class BaseField:
@property
def is_required(self) -> bool:
return (
not self.nullable and not self.has_default and not self.is_auto_primary_key
not self.nullable and not self.has_default and not self.is_auto_primary_key
)
@property

View File

@ -25,12 +25,12 @@ def create_dummy_instance(fk: Type["Model"], pk: Any = None) -> "Model":
class ForeignKey(BaseField):
def __init__(
self,
to: Type["Model"],
name: str = None,
related_name: str = None,
nullable: bool = True,
virtual: bool = False,
self,
to: Type["Model"],
name: str = None,
related_name: str = None,
nullable: bool = True,
virtual: bool = False,
) -> None:
super().__init__(nullable=nullable, name=name)
self.virtual = virtual
@ -50,7 +50,7 @@ class ForeignKey(BaseField):
return to_column.get_column_type()
def _extract_model_from_sequence(
self, value: List, child: "Model"
self, value: List, child: "Model"
) -> Union["Model", List["Model"]]:
return [self.expand_relationship(val, child) for val in value]
@ -76,10 +76,12 @@ class ForeignKey(BaseField):
def register_relation(self, model: "Model", child: "Model") -> None:
child_model_name = self.related_name or child.get_name()
model._orm_relationship_manager.add_relation(model, child, child_model_name, virtual=self.virtual)
model._orm_relationship_manager.add_relation(
model, child, child_model_name, virtual=self.virtual
)
def expand_relationship(
self, value: Any, child: "Model"
self, value: Any, child: "Model"
) -> Optional[Union["Model", List["Model"]]]:
if value is None: