3
3

More than 1 year has passed since last update.

Tortoise ORM入門

Last updated at Posted at 2021-11-05

Tortoise ORM は asyncio 互換のORM (Object Relational Mapper)です(inspired by Django)。
FastAPIでよく使われていますが、今回はFastAPIとは関係ない、純粋に Tortoise ORMの入門です。ちなみにFastAPIで使うためには、今回の範囲外ですが、Pydantic を考慮に入れる必要があります。

Tortoise ORM 公式サイト

【過去記事】
Python Asyncio入門
Aiohttpで作るSimple Web Server - Qiita
Starletteで作る Simple Web Server - QIita
FastAPIで作るWebアプリ - 基本
FastAPIで作るWebアプリ - validation
FastAPIで作るWebアプリ - Body validation
FastAPIで作るWebアプリ - Form validation
Pydantic Data Model入門
Tortoise ORM入門

まずインストールします。

pip install tortoise-orm

Modelの定義を行います。公式サイトにある簡単なものです。

model.py
from tortoise.models import Model
from tortoise import fields

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

tortoise.models.Model を使ってモデルTournamentを定義します。各フィールドはtortoise.fieldsを使って定義していきます。本記事は全体の流れを説明することを意図しているので、詳細は公式サイトに譲ります。

以下は最初に一回だけ走らせるスクリプトです。これで上のModelに対応したテーブルが作成されます。sqliteがデフォルトのDBですが、PostgreSQLやMySQLを設定することもできます。これも公式サイトに案内があります。Tortoise.generate_schemas()でスキームを作っていますが、これは一回だけ行うものです。

init.py
from tortoise import Tortoise, run_async

async def init():
    await Tortoise.init(db_url='sqlite://db.sqlite3', modules={'models': ['model']})
    # Generate the schema
    await Tortoise.generate_schemas()

# run_async is a helper function to run simple async Tortoise scripts.
run_async(init())

modulesのmodelsの値が['model']となっていますが、これはモデルの定義ファイルmodel.pyを指しています。それとコルーティンinit()を走らせるためにはtortoise.run_async()が必要になります。asyncio.run()ではエラーとなりますので注意してください。

スクリプトを実行してinit()を走らせます。

python init.py

それでは実際にORMを利用して、DB操作を行ってみます。

main.py
from tortoise import Tortoise,  run_async
from model import Tournament


async def run():
    await Tortoise.init(db_url="sqlite://db.sqlite3", modules={"models": ["model"]})

    # Create instance by save
    tournament = Tournament(name='New Tournament')
    await tournament.save()

    # Or by .create()
    await Tournament.create(name='Another Tournament')

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


run_async(run())

今回は以上です。

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3