Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow passing modules to Tortoise.init_models() #561

Merged
merged 3 commits into from
Nov 24, 2020

Conversation

lqmanh
Copy link
Contributor

@lqmanh lqmanh commented Nov 18, 2020

Description

Allow passing module objects to models_paths param of Tortoise.init_models().

Motivation and Context

Now Tortoise.init_models() only accepts models_paths as a list of strings, which may cause ConfigurationError(f'Module "{models_path}" not found') later.
With this PR, passing module objects directly instead of their paths can prevent those potential errors. Most editors/IDEs will alert developers immediatelly if there is something wrong.

Example:

Instead of:

from tortoise import Tortoise

async def main():
    await Tortoise.init(
        db_url='sqlite://db.sqlite3',
        models={'models': ['app.models']}
    )

...

Do this:

from tortoise import Tortoise
import app.models

async def main():
    await Tortoise.init(
        db_url='sqlite://db.sqlite3',
        models={'models': [app.models]}
    )

...

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 read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@lqmanh lqmanh force-pushed the features/models-with-module-objects branch from 9bce82b to d53f48c Compare November 19, 2020 02:20
@coveralls
Copy link

coveralls commented Nov 19, 2020

Pull Request Test Coverage Report for Build 380879510

  • 10 of 11 (90.91%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.03%) to 96.336%

Changes Missing Coverage Covered Lines Changed/Added Lines %
tortoise/init.py 10 11 90.91%
Totals Coverage Status
Change from base Build 375755212: -0.03%
Covered Lines: 4270
Relevant Lines: 4380

💛 - Coveralls

@lqmanh lqmanh marked this pull request as ready for review November 19, 2020 04:10
Copy link
Member

@abondar abondar left a comment

Choose a reason for hiding this comment

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

Thanks you! Looks good to me

Could you please add description to the top of changelog file?

@lqmanh lqmanh force-pushed the features/models-with-module-objects branch from 743a2df to 84bc839 Compare November 24, 2020 10:45
@lqmanh
Copy link
Contributor Author

lqmanh commented Nov 24, 2020

@abondar I've updated the changelog and added myself to contributor list.

@abondar abondar merged commit 442e870 into tortoise:develop Nov 24, 2020
@lqmanh lqmanh deleted the features/models-with-module-objects branch November 24, 2020 14:54
@abondar
Copy link
Member

abondar commented Dec 23, 2020

@lqmanh released as 0.16.19

@bnaoki
Copy link

bnaoki commented Mar 22, 2021

Hello, should the types have been updated for init() and generate_config() as well? Same with register_tortoise in tortoise.contrib.fastapi.

@lqmanh
Copy link
Contributor Author

lqmanh commented Mar 23, 2021

@bnaoki No I hadn't updated those functions. I'll create another PR when I have time.

@lqmanh
Copy link
Contributor Author

lqmanh commented Mar 23, 2021

Hey @bnaoki,
Seems like @long2ice has already helped. Many thanks!

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.

None yet

4 participants