forked from tortoise/tortoise-orm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_comments.py
More file actions
39 lines (28 loc) · 1.13 KB
/
basic_comments.py
File metadata and controls
39 lines (28 loc) · 1.13 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
and a Table definition generation with comment support
"""
from tortoise import Tortoise, fields, run_async
from tortoise.models import Model
class Event(Model):
id = fields.IntField(pk=True)
name = fields.TextField(description="Name of the event that corresponds to an action")
datetime = fields.DatetimeField(
null=True, description="Datetime of when the event was generated"
)
class Meta:
table = "event"
table_description = "This table contains a list of all the example events"
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())
await Event(name="Test 2").save()
print(await Event.all().values_list("id", flat=True))
print(await Event.all().values("id", "name"))
if __name__ == "__main__":
run_async(run())