-
Notifications
You must be signed in to change notification settings - Fork 480
Expand file tree
/
Copy pathrelations_with_unique.py
More file actions
74 lines (55 loc) · 2.55 KB
/
relations_with_unique.py
File metadata and controls
74 lines (55 loc) · 2.55 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
"""
This example shows how relations between models especially unique field work.
Key points in this example are use of ForeignKeyField and OneToOneField has to_field.
For other basic parts, it is the same as relation example.
"""
from tortoise import Tortoise, fields, run_async
from tortoise.models import Model
from tortoise.query_utils import Prefetch
class School(Model):
uuid = fields.UUIDField(primary_key=True)
name = fields.TextField()
id = fields.IntField(unique=True)
students: fields.ReverseRelation["Student"]
principal: fields.ReverseRelation["Principal"]
class Student(Model):
id = fields.IntField(primary_key=True)
name = fields.TextField()
school: fields.ForeignKeyRelation[School] = fields.ForeignKeyField(
"models.School", related_name="students", to_field="id", on_delete=fields.RESTRICT
)
class Principal(Model):
id = fields.IntField(primary_key=True)
name = fields.TextField()
school: fields.OneToOneRelation[School] = fields.OneToOneField(
"models.School", on_delete=fields.OnDelete.CASCADE, related_name="principal", to_field="id"
)
async def run():
await Tortoise.init(db_url="sqlite://:memory:", modules={"models": ["__main__"]})
await Tortoise.generate_schemas()
school1 = await School.create(id=1024, name="School1")
student1 = await Student.create(name="Sang-Heon Jeon1", school_id=school1.id)
student_schools = await Student.filter(name="Sang-Heon Jeon1").values("name", "school__name")
print(student_schools[0])
await Student.create(name="Sang-Heon Jeon2", school=school1)
school_with_filtered = (
await School.all()
.prefetch_related(Prefetch("students", queryset=Student.filter(name="Sang-Heon Jeon1")))
.first()
)
school_without_filtered = await School.first().prefetch_related("students")
print(len(school_with_filtered.students))
print(len(school_without_filtered.students))
school2 = await School.create(id=2048, name="School2")
await Student.all().update(school=school2)
student = await Student.first()
print(student.school_id)
await Student.filter(id=student1.id).update(school=school1)
schools = await School.all().order_by("students__name")
print([school.name for school in schools])
fetched_principal = await Principal.create(name="Sang-Heon Jeon3", school=school1)
print(fetched_principal.name)
fetched_school = await School.filter(name="School1").prefetch_related("principal").first()
print(fetched_school.name)
if __name__ == "__main__":
run_async(run())