forked from tortoise/tortoise-orm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic.py
More file actions
38 lines (27 loc) · 1004 Bytes
/
basic.py
File metadata and controls
38 lines (27 loc) · 1004 Bytes
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
"""
This example demonstrates most basic operations with single model
"""
from tortoise import Tortoise, fields, run_async
from tortoise.models import Model
class Event(Model):
id = fields.IntField(pk=True)
name = fields.TextField()
datetime = fields.DatetimeField(null=True)
class Meta:
table = "event"
def __str__(self):
return self.name
async def run():
await Tortoise.init(db_url="sqlite://:memory:", modules={"models": ["__main__"]})
await Tortoise.generate_schemas()
event = await Event.create(name="Test")
await Event.filter(id=event.id).update(name="Updated name")
print(await Event.filter(name="Updated name").first())
# >>> Updated name
await Event(name="Test 2").save()
print(await Event.all().values_list("id", flat=True))
# >>> [1, 2]
print(await Event.all().values("id", "name"))
# >>> [{'id': 1, 'name': 'Updated name'}, {'id': 2, 'name': 'Test 2'}]
if __name__ == "__main__":
run_async(run())