forked from tortoise/tortoise-orm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.py
More file actions
94 lines (71 loc) · 2.99 KB
/
functions.py
File metadata and controls
94 lines (71 loc) · 2.99 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
from tortoise import Tortoise, fields, run_async
from tortoise.expressions import Q
from tortoise.functions import Coalesce, Count, Length, Lower, Min, Sum, Trim, Upper
from tortoise.models import Model
class Tournament(Model):
id = fields.IntField(pk=True)
name = fields.TextField()
desc = fields.TextField(null=True)
events: fields.ReverseRelation["Event"]
def __str__(self):
return self.name
class Event(Model):
id = fields.IntField(pk=True)
name = fields.TextField()
tournament: fields.ForeignKeyRelation[Tournament] = fields.ForeignKeyField(
"models.Tournament", related_name="events"
)
participants: fields.ManyToManyRelation["Team"] = fields.ManyToManyField(
"models.Team", related_name="events", through="event_team"
)
def __str__(self):
return self.name
class Team(Model):
id = fields.IntField(pk=True)
name = fields.TextField()
events: fields.ManyToManyRelation[Event]
def __str__(self):
return self.name
async def run():
await Tortoise.init(db_url="sqlite://:memory:", modules={"models": ["__main__"]})
await Tortoise.generate_schemas()
tournament = await Tournament.create(name="New Tournament", desc="great")
await tournament.save()
await Tournament.create(name="Second tournament")
await Tournament.create(name=" final tournament ")
await Event(name="Without participants", tournament_id=tournament.id).save()
event = Event(name="Test", tournament_id=tournament.id)
await event.save()
participants = []
for i in range(2):
team = Team(name=f"Team {(i + 1)}")
await team.save()
participants.append(team)
await event.participants.add(participants[0], participants[1])
await event.participants.add(participants[0], participants[1])
print(await Tournament.all().annotate(events_count=Count("events")).filter(events_count__gte=1))
print(
await Tournament.all()
.annotate(events_count_with_filter=Count("events", _filter=Q(name="New Tournament")))
.filter(events_count_with_filter__gte=1)
)
print(await Event.filter(id=event.id).first().annotate(lowest_team_id=Min("participants__id")))
print(await Tournament.all().annotate(events_count=Count("events")).order_by("events_count"))
print(await Event.all().annotate(tournament_test_id=Sum("tournament__id")).first())
print(
await Tournament.annotate(clean_description=Coalesce("desc", "")).filter(
clean_description=""
)
)
print(
await Tournament.annotate(trimmed_name=Trim("name")).filter(trimmed_name="final tournament")
)
print(
await Tournament.annotate(name_len=Length("name")).filter(
name_len__gt=len("New Tournament")
)
)
print(await Tournament.annotate(name_lo=Lower("name")).filter(name_lo="new tournament"))
print(await Tournament.annotate(name_lo=Upper("name")).filter(name_lo="NEW TOURNAMENT"))
if __name__ == "__main__":
run_async(run())