tortoise-orm 0.25.1


pip install tortoise-orm

  Latest version

Released: Jun 05, 2025


Meta
Author: Andrey Bondar
Requires Python: >=3.9

Classifiers

Development Status
  • 3 - Alpha

Framework
  • AsyncIO

Intended Audience
  • Developers

License
  • OSI Approved :: Apache Software License

Operating System
  • MacOS :: MacOS X
  • Microsoft :: Windows
  • POSIX

Programming Language
  • PL/SQL
  • Python :: 3
  • Python :: 3.9
  • Python :: 3.10
  • Python :: 3.11
  • Python :: 3.12
  • Python :: 3.13
  • Python :: Implementation :: CPython
  • Python :: Implementation :: PyPy

Topic
  • Database
  • Software Development :: Libraries :: Python Modules
https://img.shields.io/pypi/v/tortoise-orm.svg?style=flat https://pepy.tech/badge/tortoise-orm/month https://github.com/tortoise/tortoise-orm/workflows/gh-pages/badge.svg https://github.com/tortoise/tortoise-orm/actions/workflows/ci.yml/badge.svg?branch=develop https://coveralls.io/repos/github/tortoise/tortoise-orm/badge.svg https://app.codacy.com/project/badge/Grade/844030d0cb8240d6af92c71bfac764ff

Introduction

Tortoise ORM is an easy-to-use asyncio ORM (Object Relational Mapper) inspired by Django.

You can find the docs at Documentation

Tortoise ORM supports CPython 3.9 and later for SQLite, MySQL, PostgreSQL, Microsoft SQL Server, and Oracle.

Why was Tortoise ORM built?

Tortoise ORM was built to provide a lightweight, async-native Object-Relational Mapper for Python with a familiar Django-like API.

Tortoise ORM performs well when compared to other Python ORMs. In our benchmarks, where we measure different read and write operations (rows/sec, more is better), it’s trading places with Pony ORM:

https://raw.githubusercontent.com/tortoise/tortoise-orm/develop/docs/ORM_Perf.png

How is an ORM useful?

An Object-Relational Mapper (ORM) abstracts database interactions, allowing developers to work with databases using high-level, object-oriented code instead of raw SQL.

  • Reduces boilerplate SQL, allowing faster development with cleaner, more readable code.

  • Helps prevent SQL injection by using parameterized queries.

  • Centralized schema and relationship definitions make code easier to manage and modify.

  • Handles schema changes through version-controlled migrations.

Getting Started

Installation

The following table shows the available installation options for different databases (note that there are multiple options of clients for some databases):

Available Installation Options

Database

Installation Command

SQLite

pip install tortoise-orm

PostgreSQL (psycopg)

pip install tortoise-orm[psycopg]

PostgreSQL (asyncpg)

pip install tortoise-orm[asyncpg]

MySQL (aiomysql)

pip install tortoise-orm[aiomysql]

MySQL (asyncmy)

pip install tortoise-orm[asyncmy]

MS SQL

pip install tortoise-orm[asyncodbc]

Oracle

pip install tortoise-orm[asyncodbc]

Quick Tutorial

Define the models by inheriting from tortoise.models.Model.

from tortoise.models import Model
from tortoise import fields

class Tournament(Model):
    id = fields.IntField(primary_key=True)
    name = fields.CharField(max_length=20)


class Event(Model):
    id = fields.BigIntField(primary_key=True)
    name = fields.TextField()
    tournament = fields.ForeignKeyField('models.Tournament', related_name='events', on_delete=fields.OnDelete.CASCADE)
    participants = fields.ManyToManyField('models.Team', related_name='events', through='event_team', on_delete=fields.OnDelete.SET_NULL)


class Team(Model):
    id = fields.UUIDField(primary_key=True)
    name = fields.CharField(max_length=20, unique=True)

After defining the models, Tortoise ORM needs to be initialized to establish the relationships between models and connect to the database. The code below creates a connection to a SQLite DB database with the aiosqlite client. generate_schema sets up schema on an empty database. generate_schema is for development purposes only, check out aerich or other migration tools for production use.

from tortoise import Tortoise, run_async

async def init():
    # Here we connect to a SQLite DB file.
    # also specify the app name of "models"
    # which contain models from "app.models"
    await Tortoise.init(
        db_url='sqlite://db.sqlite3',
        modules={'models': ['app.models']}
    )
    # Generate the schema
    await Tortoise.generate_schemas()

run_async(main())

run_async is a helper function to run simple Tortoise scripts. Check out Documentation for FastAPI, Sanic and other integrations.

With the Tortoise initialized, the models are available for use:

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

    # Creating an instance with .save()
    tournament = Tournament(name='New Tournament')
    await tournament.save()

    # Or with .create()
    await Event.create(name='Without participants', tournament=tournament)
    event = await Event.create(name='Test', tournament=tournament)
    participants = []
    for i in range(2):
        team = await Team.create(name='Team {}'.format(i + 1))
        participants.append(team)

    # Many to Many Relationship management is quite straightforward
    # (there are .remove(...) and .clear() too)
    await event.participants.add(*participants)

    # Iterate over related entities with the async context manager
    async for team in event.participants:
        print(team.name)

    # The related entities are cached and can be iterated in the synchronous way afterwards
    for team in event.participants:
        pass

    # Use prefetch_related to fetch related objects
    selected_events = await Event.filter(
        participants=participants[0].id
    ).prefetch_related('participants', 'tournament')
    for event in selected_events:
        print(event.tournament.name)
        print([t.name for t in event.participants])

    # Prefetch multiple levels of related entities
    await Team.all().prefetch_related('events__tournament')

    # Filter and order by related models too
    await Tournament.filter(
        events__name__in=['Test', 'Prod']
    ).order_by('-events__participants__name').distinct()

run_async(main())

Learn more at the documentation site

Migration

Tortoise ORM uses Aerich as its database migration tool, see more detail at its docs.

Contributing

Please have a look at the Contribution Guide.

ThanksTo

Powerful Python IDE Pycharm from Jetbrains.

https://resources.jetbrains.com/storage/products/company/brand/logos/jb_beam.svg

License

This project is licensed under the Apache License - see the LICENSE.txt file for details.

0.25.1 Jun 05, 2025
0.25.0 Apr 14, 2025
0.24.2 Mar 03, 2025
0.24.1 Feb 24, 2025
0.24.0 Jan 23, 2025
0.23.0 Dec 23, 2024
0.22.2 Dec 09, 2024
0.22.1 Nov 25, 2024
0.22.0 Nov 19, 2024
0.21.7 Oct 14, 2024
0.21.6 Aug 17, 2024
0.21.5 Jul 19, 2024
0.21.4 Jul 03, 2024
0.21.3 Jun 01, 2024
0.21.2 May 25, 2024
0.21.1 May 24, 2024
0.21.0 May 23, 2024
0.20.1 Apr 26, 2024
0.20.0 Aug 11, 2023
0.19.3 Jan 30, 2023
0.19.2 Jul 11, 2022
0.19.1 May 20, 2022
0.19.0 Mar 27, 2022
0.18.1 Jan 10, 2022
0.18.0 Dec 20, 2021
0.17.8 Oct 06, 2021
0.17.7 Aug 31, 2021
0.17.6 Jul 26, 2021
0.17.5 Jul 07, 2021
0.17.4 Jun 03, 2021
0.17.3 May 23, 2021
0.17.2 Apr 09, 2021
0.17.1 Mar 27, 2021
0.17.0 Mar 20, 2021
0.16.21 Feb 04, 2021
0.16.20 Jan 23, 2021
0.16.19 Dec 23, 2020
0.16.18 Nov 16, 2020
0.16.17 Oct 23, 2020
0.16.16 Sep 24, 2020
0.16.15 Sep 16, 2020
0.16.14 Jul 25, 2020
0.16.13 Jun 02, 2020
0.16.12 May 22, 2020
0.16.11 May 14, 2020
0.16.10 Apr 30, 2020
0.16.9 Apr 26, 2020
0.16.8 Apr 22, 2020
0.16.7 Apr 19, 2020
0.16.6 Apr 18, 2020
0.16.5 Apr 10, 2020
0.16.4 Apr 01, 2020
0.16.3 Mar 26, 2020
0.16.2 Mar 22, 2020
0.16.1 Mar 18, 2020
0.16.0 Mar 12, 2020
0.15.24 Apr 22, 2020
0.15.23 Apr 18, 2020
0.15.22 Apr 10, 2020
0.15.21 Mar 26, 2020
0.15.20 Mar 22, 2020
0.15.19 Mar 11, 2020
0.15.18 Feb 29, 2020
0.15.17 Feb 25, 2020
0.15.16 Feb 25, 2020
0.15.15 Feb 22, 2020
0.15.14 Feb 21, 2020
0.15.13 Feb 15, 2020
0.15.12 Feb 11, 2020
0.15.11 Feb 10, 2020
0.15.10 Feb 10, 2020
0.15.9 Jan 19, 2020
0.15.8 Jan 17, 2020
0.15.7 Dec 23, 2019
0.15.6 Dec 20, 2019
0.15.5 Dec 09, 2019
0.15.4 Dec 02, 2019
0.15.3 Nov 28, 2019
0.15.2 Nov 27, 2019
0.15.1 Nov 25, 2019
0.15.0 Nov 23, 2019
0.14.2 Nov 11, 2019
0.14.1 Nov 07, 2019
0.14.0 Oct 27, 2019
0.13.12 Oct 10, 2019
0.13.11 Oct 10, 2019
0.13.10 Oct 01, 2019
0.13.9 Oct 01, 2019
0.13.8 Sep 25, 2019
0.13.7 Sep 23, 2019
0.13.6 Sep 21, 2019
0.13.5 Sep 11, 2019
0.13.4 Sep 06, 2019
0.13.3 Sep 05, 2019
0.13.2 Sep 01, 2019
0.13.1 Aug 26, 2019
0.13.0 Aug 15, 2019
0.12.8 Jul 25, 2019
0.12.7 Jul 23, 2019
0.12.6 Jul 21, 2019
0.12.5 Jul 10, 2019
0.12.4 Jul 07, 2019
0.12.3 Jul 05, 2019
0.12.2 Jun 18, 2019
0.12.1 Jun 15, 2019
0.12.0 Jun 11, 2019
0.11.13 Jun 05, 2019
0.11.12 May 26, 2019
0.11.11 May 15, 2019
0.11.10 May 02, 2019
0.11.9 Apr 18, 2019
0.11.8 Apr 01, 2019
0.11.7 Mar 23, 2019
0.11.6 Mar 20, 2019
0.11.5 Mar 20, 2019
0.11.4 Feb 20, 2019
0.11.3 Feb 06, 2019
0.11.2 Jan 06, 2019
0.11.1 Dec 25, 2018
0.11.0 Dec 24, 2018
0.10.11 Nov 16, 2018
0.10.10 Oct 31, 2018
0.10.9 Oct 17, 2018
0.10.8 Oct 01, 2018
0.10.7 Sep 25, 2018
0.10.6 Sep 12, 2018
0.10.5 Sep 10, 2018
0.10.4 Sep 08, 2018
0.10.3 Sep 04, 2018
0.10.2 Aug 31, 2018
0.10.1 Aug 01, 2018
0.10.0 Aug 01, 2018
0.9.4 Jul 17, 2018
0.9.2 Jul 06, 2018
0.9.1 Jul 02, 2018
0.9.0 Jul 02, 2018
0.8.2 Jun 26, 2018
0.8.1 Jun 26, 2018
0.8.0 Jun 26, 2018
0.7.10 May 29, 2018
0.7.9 Apr 26, 2018
0.7.8 Apr 26, 2018
0.7.7 Apr 20, 2018
0.7.6 Apr 20, 2018
0.7.5 Apr 19, 2018
0.7.4 Apr 17, 2018
0.7.3 Apr 17, 2018
0.7.2 Apr 17, 2018
0.7.1 Apr 17, 2018
0.7.0 Apr 16, 2018
0.6.2 Apr 16, 2018
0.6.1 Apr 16, 2018
0.6.0 Apr 16, 2018
0.5.5 Apr 16, 2018
0.5.4 Apr 13, 2018
0.5.3 Apr 13, 2018
0.5.2 Apr 13, 2018
0.5.1 Apr 13, 2018
0.5.0 Apr 13, 2018
0.4.0 Apr 12, 2018
0.3.3 Apr 11, 2018
0.3.2 Apr 11, 2018
0.3.1 Apr 04, 2018
0.3.0 Apr 04, 2018
0.2.0 Mar 30, 2018
0.1.3 Mar 30, 2018
0.1.2 Mar 29, 2018
0.1.1 Mar 29, 2018
0.1.0 Mar 29, 2018

Wheel compatibility matrix

Platform Python 3
any

Files in release

Extras:
Dependencies:
aiosqlite (<1.0.0,>=0.16.0)
iso8601 (<3.0.0,>=2.1.0)
pypika-tortoise (<1.0.0,>=0.6.1)
pytz