You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.rst
+85-96Lines changed: 85 additions & 96 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -20,16 +20,13 @@ Introduction
20
20
21
21
Tortoise ORM is an easy-to-use ``asyncio`` ORM *(Object Relational Mapper)* inspired by Django.
22
22
23
-
Tortoise ORM was built with relations in mind and admiration for the excellent and popular Django ORM.
24
-
It's engraved in its design that you are working not with just tables, you work with relational data.
25
-
26
23
You can find the docs at `Documentation <https://tortoise.github.io>`_
27
24
28
25
.. note::
29
26
Tortoise ORM is a young project and breaking changes are to be expected.
30
27
We keep a `Changelog <https://tortoise.github.io/CHANGELOG.html>`_ and it will have possible breakage clearly documented.
31
28
32
-
Tortoise ORM is supported on CPython >= 3.9 for SQLite, MySQL and PostgreSQL and Microsoft SQL Server and Oracle.
29
+
Tortoise ORM supports CPython 3.9 and later for SQLite, MySQL, PostgreSQL, Microsoft SQL Server, and Oracle.
33
30
34
31
Why was Tortoise ORM built?
35
32
---------------------------
@@ -51,48 +48,47 @@ It also performs well when compared to other Python ORMs. In `our benchmarks <ht
51
48
How is an ORM useful?
52
49
---------------------
53
50
54
-
When you build an application or service that uses a relational database, there is a point where you can't get away with just using parameterized queries or even query builder. You just keep repeating yourself, writing slightly different code for each entity.
55
-
Code has no idea about relations between data, so you end up concatenating your data almost manually.
56
-
It is also easy to make mistakes in how you access your database, which can be exploited by SQL-injection attacks.
57
-
Your data rules are also distributed, increasing the complexity of managing your data, and even worse, could lead to those rules being applied inconsistently.
51
+
An Object-Relational Mapper (ORM) abstracts database interactions, allowing developers to work with databases using high-level, object-oriented code instead of raw SQL.
58
52
59
-
An ORM (Object Relational Mapper) is designed to address these issues, by centralising your data model and data rules, ensuring that your data is managed safely (providing immunity to SQL-injection) and keeping track of relationships so you don't have to.
53
+
* Reduces boilerplate SQL, allowing faster development with cleaner, more readable code.
54
+
* Helps prevent SQL injection by using parameterized queries.
55
+
* Centralized schema and relationship definitions make code easier to manage and modify.
56
+
* Handles schema changes through version-controlled migrations.
60
57
61
58
Getting Started
62
59
===============
63
60
64
61
Installation
65
62
------------
66
-
First you have to install Tortoise ORM like this:
67
-
68
-
.. code-block:: bash
69
-
70
-
pip install tortoise-orm
71
-
72
-
You can also install with your db driver (`aiosqlite` is builtin):
73
-
74
-
.. code-block:: bash
75
-
76
-
pip install "tortoise-orm[asyncpg]"
77
-
78
-
79
-
For `MySQL`:
80
63
81
-
.. code-block:: bash
64
+
The following table shows the available installation options for different databases (note that there are multiple options of clients for some databases):
65
+
66
+
.. list-table:: Available Installation Options
67
+
:header-rows: 1
68
+
:widths: 30 70
69
+
70
+
* - Database
71
+
- Installation Command
72
+
* - SQLite
73
+
- ``pip install tortoise-orm``
74
+
* - PostgreSQL (psycopg)
75
+
- ``pip install tortoise-orm[psycopg]``
76
+
* - PostgreSQL (asyncpg)
77
+
- ``pip install tortoise-orm[asyncpg]``
78
+
* - MySQL (aiomysql)
79
+
- ``pip install tortoise-orm[aiomysql]``
80
+
* - MySQL (asyncmy)
81
+
- ``pip install tortoise-orm[asyncmy]``
82
+
* - MS SQL
83
+
- ``pip install tortoise-orm[asyncodbc]``
84
+
* - Oracle
85
+
- ``pip install tortoise-orm[asyncodbc]``
82
86
83
-
pip install "tortoise-orm[asyncmy]"
84
-
85
-
For `Microsoft SQL Server`/`Oracle` (**not fully tested**):
86
-
87
-
.. code-block:: bash
88
-
89
-
pip install "tortoise-orm[asyncodbc]"
90
87
91
88
Quick Tutorial
92
89
--------------
93
90
94
-
The primary entity of tortoise is ``tortoise.models.Model``.
95
-
You can start writing models like this:
91
+
Define the models by inheriting from ``tortoise.models.Model``.
96
92
97
93
98
94
.. code-block:: python3
@@ -104,35 +100,26 @@ You can start writing models like this:
After you defined all your models, tortoise needs you to init them, in order to create backward relations between models and match your db client with the appropriate models.
130
115
131
-
You can do it like this:
116
+
After defining the models, Tortoise ORM needs to be initialized to establish the relationships between models and connect to the database.
117
+
The code below creates a connection to a SQLite DB database with the ``aiosqlite`` client. ``generate_schema`` sets up schema on an empty database.
118
+
``generate_schema`` is for development purposes only, check out ``aerich`` or other migration tools for production use.
132
119
133
120
.. code-block:: python3
134
121
135
-
from tortoise import Tortoise
122
+
from tortoise import Tortoise, run_async
136
123
137
124
async def init():
138
125
# Here we connect to a SQLite DB file.
@@ -145,64 +132,66 @@ You can do it like this:
145
132
# Generate the schema
146
133
await Tortoise.generate_schemas()
147
134
135
+
run_async(main())
148
136
149
-
Here we create a connection to an SQLite database in the local directory called ``db.sqlite3``. Then we discover and initialise the models.
137
+
``run_async`` is a helper function to run simple Tortoise scripts. Check out `Documentation <https://tortoise.github.io>`_ for FastAPI, Sanic and other integrations.
150
138
151
-
Tortoise ORM currently supports the following databases:
139
+
With the Tortoise initialized, the models are available for use:
Copy file name to clipboardExpand all lines: docs/CONTRIBUTING.rst
-6Lines changed: 0 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -24,12 +24,6 @@ If not you are welcome to open one.
24
24
If you have an incomplete change, but won't/can't continue working on it, please create a PR in any case and mark it as ``(WIP)`` so we can help each other.
25
25
26
26
27
-
Have a chat
28
-
===========
29
-
30
-
We have a chatroom on `Gitter <https://gitter.im/tortoise/community>`_
0 commit comments