forked from tortoise/tortoise-orm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostgres.py
More file actions
45 lines (36 loc) · 1.15 KB
/
postgres.py
File metadata and controls
45 lines (36 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
"""
This example showcases postgres features
"""
from tortoise import Tortoise, fields, run_async
from tortoise.models import Model
class Report(Model):
id = fields.IntField(primary_key=True)
content = fields.JSONField()
def __str__(self):
return str(self.id)
async def run():
await Tortoise.init(
{
"connections": {
"default": {
"engine": "tortoise.backends.asyncpg",
"credentials": {
"host": "localhost",
"port": "5432",
"user": "tortoise",
"password": "qwerty123",
"database": "test",
},
}
},
"apps": {"models": {"models": ["__main__"], "default_connection": "default"}},
},
_create_db=True,
)
await Tortoise.generate_schemas()
report_data = {"foo": "bar"}
print(await Report.create(content=report_data))
print(await Report.filter(content=report_data).first())
await Tortoise._drop_databases()
if __name__ == "__main__":
run_async(run())