Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions tests/migrations/test_schema_editor_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,12 @@ class Meta:
table = "tag"
app = "models"


class WidgetWithTags(Model):
id = fields.IntField(pk=True)
tags = fields.ManyToManyField("models.Tag", related_name="widgets")
tags = fields.ManyToManyField(Tag, related_name="widgets")

class Meta:
class Meta: # type: ignore
table = "widget"
app = "models"

Expand Down
8 changes: 5 additions & 3 deletions tests/testmodels.py
Original file line number Diff line number Diff line change
Expand Up @@ -1152,7 +1152,9 @@ class Flavor(Model):
class Drink(Model):
id = fields.IntField(pk=True)
name = fields.CharField(max_length=100)
flavors = fields.ManyToManyField("models.Flavor", related_name="drinks", through="drink_flavor")
toppings = fields.ManyToManyField(
"models.Flavor", related_name="topping_drinks", through="drink_topping"
flavors = fields.ManyToManyField(
Flavor, related_name="drinks", through="drink_flavor"
)
toppings = fields.ManyToManyField(
Flavor, related_name="topping_drinks", through="drink_topping"
)
24 changes: 12 additions & 12 deletions tortoise/fields/relational.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from collections.abc import AsyncGenerator, Generator, Iterator
from typing import TYPE_CHECKING, Any, Generic, Literal, TypeVar, overload

from pypika_tortoise import Table
from pypika_tortoise.queries import Table

from tortoise.exceptions import ConfigurationError, NoValuesFetched, OperationalError
from tortoise.fields.base import CASCADE, SET_NULL, Field, OnDelete
Expand Down Expand Up @@ -358,7 +358,7 @@ def __init__(
class OneToOneFieldInstance(ForeignKeyFieldInstance[MODEL]):
def __init__(
self,
model_name: type[Model] | str,
model_name: type[MODEL] | str,
related_name: str | None | Literal[False] = None,
on_delete: OnDelete = CASCADE,
**kwargs: Any,
Expand All @@ -381,7 +381,7 @@ class ManyToManyFieldInstance(RelationalField[MODEL]):

def __init__(
self,
model_name: type[Model] | str,
model_name: type[MODEL] | str,
through: str | None = None,
forward_key: str | None = None,
backward_key: str = "",
Expand Down Expand Up @@ -421,7 +421,7 @@ def describe(self, serializable: bool) -> dict:
if isinstance(self.model_name, str):
model_name = self.model_name
else:
model: type[Model] = self.model_name
model: type[MODEL] = self.model_name
model_name = f"{model._meta.app}.{model.__name__}"
desc["model_name"] = model_name
desc["related_name"] = self.related_name
Expand All @@ -435,7 +435,7 @@ def describe(self, serializable: bool) -> dict:

@overload
def OneToOneField(
to: type[Model] | str,
to: type[MODEL] | str,
related_name: str | None | Literal[False] = None,
on_delete: OnDelete = CASCADE,
db_constraint: bool = True,
Expand All @@ -447,7 +447,7 @@ def OneToOneField(

@overload
def OneToOneField(
to: type[Model] | str,
to: type[MODEL] | str,
related_name: str | None | Literal[False] = None,
on_delete: OnDelete = CASCADE,
db_constraint: bool = True,
Expand All @@ -457,7 +457,7 @@ def OneToOneField(


def OneToOneField(
to: type[Model] | str,
to: type[MODEL] | str,
related_name: str | None | Literal[False] = None,
on_delete: OnDelete = CASCADE,
db_constraint: bool = True,
Expand Down Expand Up @@ -510,7 +510,7 @@ def OneToOneField(

@overload
def ForeignKeyField(
to: type[Model] | str,
to: type[MODEL] | str,
related_name: str | None | Literal[False] = None,
on_delete: OnDelete = CASCADE,
db_constraint: bool = True,
Expand All @@ -522,7 +522,7 @@ def ForeignKeyField(

@overload
def ForeignKeyField(
to: type[Model] | str,
to: type[MODEL] | str,
related_name: str | None | Literal[False] = None,
on_delete: OnDelete = CASCADE,
db_constraint: bool = True,
Expand All @@ -532,7 +532,7 @@ def ForeignKeyField(


def ForeignKeyField(
to: type[Model] | str,
to: type[MODEL] | str,
related_name: str | None | Literal[False] = None,
on_delete: OnDelete = CASCADE,
db_constraint: bool = True,
Expand Down Expand Up @@ -584,7 +584,7 @@ def ForeignKeyField(


def ManyToManyField(
to: type[Model] | str,
to: type[MODEL] | str,
through: str | None = None,
forward_key: str | None = None,
backward_key: str = "",
Expand All @@ -593,7 +593,7 @@ def ManyToManyField(
db_constraint: bool = True,
unique: bool = True,
**kwargs: Any,
) -> ManyToManyRelation[Any]:
) -> ManyToManyRelation[MODEL]:
"""
ManyToMany relation field.

Expand Down
Loading