Skip to content

Conversation

@waketzheng
Copy link
Contributor

@waketzheng waketzheng commented Dec 1, 2025

Description

  • Before this PR:
from tortoise import Model, fields

class Group(Model):
    name = fields.CharField(20)

class Role(Model):
    name = fields.CharField(20)

class User(Model):
    group = fields.ForeignkeyField('models.Group')
    roles = fields.ManyToManyField('models.Role')
  • After this PR merged:
from tortoise import Model, fields

class Group(Model):
    name = fields.CharField(20)

class Role(Model):
    name = fields.CharField(20)

class User(Model):
    group = fields.ForeignkeyField(Group)
    roles = fields.ManyToManyField(Role)

Motivation and Context

To support Django style foreignkey/o2o/m2m

How Has This Been Tested?

make ci

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added the changelog accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@codspeed-hq
Copy link

codspeed-hq bot commented Dec 1, 2025

CodSpeed Performance Report

Merging #2027 will not alter performance

Comparing waketzheng:feat-foreignkey-to-model (01a9de2) with develop (a725917)

Summary

✅ 16 untouched

@coveralls
Copy link

coveralls commented Dec 1, 2025

Pull Request Test Coverage Report for Build 20420146382

Details

  • 23 of 29 (79.31%) changed or added relevant lines in 2 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.07%) to 90.677%

Changes Missing Coverage Covered Lines Changed/Added Lines %
tortoise/init.py 8 9 88.89%
tortoise/fields/relational.py 15 20 75.0%
Totals Coverage Status
Change from base Build 20418916800: -0.07%
Covered Lines: 6648
Relevant Lines: 7216

💛 - Coveralls

Copy link
Contributor

@henadzit henadzit left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a great addition!

Does it actually work though? Can you please add tests with "self" and classes passed to relations. Thanks!

def validate_model_name(cls, model_name: str | type[Model]) -> None:
if not isinstance(model_name, str):
model_class: type[Model] = model_name
if not hasattr(model_class, "_meta"):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can check for that more precisely:

issubclass(child_type, parent_type)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parent_type is tortoise.models.Model, which will cause a import cycle if I use issubclass

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants