fixed in general

This commit is contained in:
collerek
2020-12-15 14:11:46 +01:00
parent 1b42d321b9
commit daf47f891e
9 changed files with 71 additions and 27 deletions

View File

@ -89,18 +89,28 @@ def register_reverse_model_fields(
) -> None:
if issubclass(model_field, ManyToManyField):
model.Meta.model_fields[child_model_name] = ManyToMany(
child, through=model_field.through, name=child_model_name, virtual=True, related_name=model_field.name
child,
through=model_field.through,
name=child_model_name,
virtual=True,
related_name=model_field.name,
)
# register foreign keys on through model
adjust_through_many_to_many_model(model, child, model_field)
adjust_through_many_to_many_model(model, child, model_field, child_model_name)
else:
model.Meta.model_fields[child_model_name] = ForeignKey(
child, real_name=child_model_name, virtual=True, related_name=model_field.name
child,
real_name=child_model_name,
virtual=True,
related_name=model_field.name,
)
def adjust_through_many_to_many_model(
model: Type["Model"], child: Type["Model"], model_field: Type[ManyToManyField]
model: Type["Model"],
child: Type["Model"],
model_field: Type[ManyToManyField],
child_model_name: str,
) -> None:
model_field.through.Meta.model_fields[model.get_name()] = ForeignKey(
model, real_name=model.get_name(), ondelete="CASCADE"
@ -148,6 +158,7 @@ def create_and_append_m2m_fk(
model.Meta.tablename + "." + model.get_column_alias(model.Meta.pkname),
ondelete="CASCADE",
onupdate="CASCADE",
related_name=model_field.name,
),
)
model_field.through.Meta.columns.append(column)

View File

@ -62,11 +62,17 @@ class ModelTableProxy:
@staticmethod
def get_clause_target_and_filter_column_name(
parent_model: Type["Model"], target_model: Type["Model"], reverse: bool, related: str,
parent_model: Type["Model"],
target_model: Type["Model"],
reverse: bool,
related: str,
) -> Tuple[Type["Model"], str]:
if reverse:
field = parent_model.Meta.model_fields[related]
# field = target_model.resolve_relation_field(target_model, parent_model)
field_name = (
parent_model.Meta.model_fields[related].related_name
or parent_model.get_name() + "s"
)
field = target_model.Meta.model_fields[field_name]
if issubclass(field, ormar.fields.ManyToManyField):
sub_field = target_model.resolve_relation_field(
field.through, parent_model