tortoise/tortoise-orm

Usage with Tornado (was: No DB associated to model)

Open

#99 aperta il 3 feb 2019

Vedi su GitHub
 (17 commenti) (0 reazioni) (0 assegnatari)Python (333 fork)batch import
documentationgood first issuepapercut

Metriche repository

Star
 (3863 star)
Metriche merge PR
 (Merge medio 15g 7h) (9 PR mergiate in 30 g)

Descrizione

from tortoise.models import Model
from tortoise.fields import *

class Tournament(Model):
    id = IntField(pk=True)
    name = TextField()

if __name__ == "__main__":
    from tortoise import Tortoise, run_async


    async def init():
        # Here we create a SQLite DB using file "db.sqlite3"
        #  also specify the app name of "models"
        #  which contain models from "app.models"
        await Tortoise.init(
            db_url='sqlite://otc.db',
            modules={'models': ['model']} # 传入模块名称,本模块名为 model
        )
        # Generate the schema
        await Tortoise.generate_schemas()


    run_async(init())

    async def dps():
        await Tournament.create(name='Another Tournament')

        # Now search for a record
        tour = await Tournament.filter(name__contains='Another').first()
        print(tour.name)
    run_async(dps())

============================== tortoise.exceptions.ConfigurationError: No DB associated to model

WHY ?

Guida contributor