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
39 lines (28 loc) · 1.04 KB
/
Copy pathbasic.py
File metadata and controls
39 lines (28 loc) · 1.04 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
"""
This example demonstrates most basic operations with single model
"""
from tortoise import fields, run_async
from tortoise.contrib.test import init_memory_sqlite
from tortoise.models import Model
class Event(Model):
id = fields.IntField(primary_key=True)
name = fields.TextField()
datetime = fields.DatetimeField(null=True)
username = fields.CharField(max_length=150, unique=True, db_index=True)
class Meta:
table = "event"
def __str__(self):
return self.name
@init_memory_sqlite
async def run() -> None:
event = await Event.create(name="Test", username="test")
await Event.filter(id=event.id).update(name="Updated name")
print(await Event.filter(username="test").first())
# >>> Updated name
await Event(name="Test 2", username="test2").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())