From 0b53759ba8d11ccdf7cd618d9bb3f6918dbdc120 Mon Sep 17 00:00:00 2001 From: ropmyung Date: Thu, 26 Mar 2026 18:11:33 +0900 Subject: [PATCH 1/4] Applies model generics on relational fields functions --- tortoise/fields/relational.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tortoise/fields/relational.py b/tortoise/fields/relational.py index 2808848e5..b9297cbbf 100644 --- a/tortoise/fields/relational.py +++ b/tortoise/fields/relational.py @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, From eb0099103b0f2259c9ea8155ed16fb06761d5d1c Mon Sep 17 00:00:00 2001 From: ropmyung Date: Mon, 20 Apr 2026 20:44:52 +0900 Subject: [PATCH 2/4] fix: update type hints for model_name in relational fields --- tortoise/fields/relational.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tortoise/fields/relational.py b/tortoise/fields/relational.py index b9297cbbf..c5bba4e78 100644 --- a/tortoise/fields/relational.py +++ b/tortoise/fields/relational.py @@ -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, @@ -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 = "", @@ -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 @@ -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 = "", @@ -593,7 +593,7 @@ def ManyToManyField( db_constraint: bool = True, unique: bool = True, **kwargs: Any, -) -> ManyToManyRelation[Any]: +) -> ManyToManyRelation[MODEL]: """ ManyToMany relation field. From ce2127b140a3d98ec0d94959966975129c6d79dc Mon Sep 17 00:00:00 2001 From: ropmyung Date: Sat, 25 Apr 2026 11:51:47 +0900 Subject: [PATCH 3/4] fix: update tests Co-authored-by: Copilot --- tests/migrations/test_schema_editor_sql.py | 7 +++++-- tests/testmodels.py | 8 +++++--- tortoise/fields/relational.py | 2 +- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/tests/migrations/test_schema_editor_sql.py b/tests/migrations/test_schema_editor_sql.py index 77a0be54e..477973798 100644 --- a/tests/migrations/test_schema_editor_sql.py +++ b/tests/migrations/test_schema_editor_sql.py @@ -87,11 +87,14 @@ class Meta: table = "tag" app = "models" + class WidgetWithTags(Model): id = fields.IntField(pk=True) - tags = fields.ManyToManyField("models.Tag", related_name="widgets") + tags: fields.ManyToManyRelation[Tag] = fields.ManyToManyField( + "models.Tag", related_name="widgets" + ) - class Meta: + class Meta: # type: ignore table = "widget" app = "models" diff --git a/tests/testmodels.py b/tests/testmodels.py index 24ca2fa98..b837f5f09 100644 --- a/tests/testmodels.py +++ b/tests/testmodels.py @@ -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" + ) \ No newline at end of file diff --git a/tortoise/fields/relational.py b/tortoise/fields/relational.py index c5bba4e78..442707848 100644 --- a/tortoise/fields/relational.py +++ b/tortoise/fields/relational.py @@ -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 From 99c0c195f4470c021c5695f8c988c8e832a7c830 Mon Sep 17 00:00:00 2001 From: ropmyung Date: Sat, 25 Apr 2026 11:54:13 +0900 Subject: [PATCH 4/4] fix: update incorrect test application --- tests/migrations/test_schema_editor_sql.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/migrations/test_schema_editor_sql.py b/tests/migrations/test_schema_editor_sql.py index 477973798..8bffba85d 100644 --- a/tests/migrations/test_schema_editor_sql.py +++ b/tests/migrations/test_schema_editor_sql.py @@ -90,9 +90,7 @@ class Meta: class WidgetWithTags(Model): id = fields.IntField(pk=True) - tags: fields.ManyToManyRelation[Tag] = fields.ManyToManyField( - "models.Tag", related_name="widgets" - ) + tags = fields.ManyToManyField(Tag, related_name="widgets") class Meta: # type: ignore table = "widget"