-
-
Notifications
You must be signed in to change notification settings - Fork 390
/
testmodels.py
979 lines (715 loc) · 28.3 KB
/
testmodels.py
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
"""
This is the testing Models
"""
import binascii
import datetime
import os
import re
import uuid
from decimal import Decimal
from enum import Enum, IntEnum
from typing import List, Union
import pytz
from pydantic import BaseModel, ConfigDict
from tortoise import fields
from tortoise.exceptions import ValidationError
from tortoise.fields import NO_ACTION
from tortoise.manager import Manager
from tortoise.models import Model
from tortoise.queryset import QuerySet
from tortoise.validators import (
CommaSeparatedIntegerListValidator,
MaxValueValidator,
MinValueValidator,
RegexValidator,
validate_ipv4_address,
validate_ipv6_address,
)
def generate_token():
return binascii.hexlify(os.urandom(16)).decode("ascii")
class TestSchemaForJSONField(BaseModel):
foo: int
bar: str
__test__ = False
json_pydantic_default = TestSchemaForJSONField(foo=1, bar="baz")
class Author(Model):
name = fields.CharField(max_length=255)
class Book(Model):
name = fields.CharField(max_length=255)
author: fields.ForeignKeyRelation[Author] = fields.ForeignKeyField(
"models.Author", related_name="books"
)
rating = fields.FloatField()
subject = fields.CharField(max_length=255, null=True)
class BookNoConstraint(Model):
name = fields.CharField(max_length=255)
author: fields.ForeignKeyRelation[Author] = fields.ForeignKeyField(
"models.Author", db_constraint=False
)
rating = fields.FloatField()
class Tournament(Model):
id = fields.SmallIntField(primary_key=True)
name = fields.CharField(max_length=255)
desc = fields.TextField(null=True)
created = fields.DatetimeField(auto_now_add=True, db_index=True)
events: fields.ReverseRelation["Event"]
minrelations: fields.ReverseRelation["MinRelation"]
uniquetogetherfieldswithfks: fields.ReverseRelation["UniqueTogetherFieldsWithFK"]
class PydanticMeta:
exclude = ("minrelations", "uniquetogetherfieldswithfks")
def __str__(self):
return self.name
class Reporter(Model):
"""Whom is assigned as the reporter"""
id = fields.IntField(primary_key=True)
name = fields.TextField()
events: fields.ReverseRelation["Event"]
class Meta:
table = "re_port_er"
def __str__(self):
return self.name
class Event(Model):
"""Events on the calendar"""
event_id = fields.BigIntField(primary_key=True)
#: The name
name = fields.TextField()
#: What tournaments is a happenin'
tournament: fields.ForeignKeyRelation["Tournament"] = fields.ForeignKeyField(
"models.Tournament", related_name="events"
)
reporter: fields.ForeignKeyNullableRelation[Reporter] = fields.ForeignKeyField(
"models.Reporter", null=True
)
participants: fields.ManyToManyRelation["Team"] = fields.ManyToManyField(
"models.Team",
related_name="events",
through="event_team",
backward_key="idEvent",
)
modified = fields.DatetimeField(auto_now=True)
token = fields.TextField(default=generate_token)
alias = fields.IntField(null=True)
class Meta:
ordering = ["name"]
def __str__(self):
return self.name
class Node(Model):
name = fields.CharField(max_length=10)
class Tree(Model):
parent: fields.ForeignKeyRelation[Node] = fields.ForeignKeyField(
"models.Node", related_name="parent_trees"
)
child: fields.ForeignKeyRelation[Node] = fields.ForeignKeyField(
"models.Node", related_name="children_trees", on_delete=NO_ACTION
)
class Address(Model):
city = fields.CharField(max_length=64)
street = fields.CharField(max_length=128)
event: fields.OneToOneRelation[Event] = fields.OneToOneField(
"models.Event",
on_delete=fields.CASCADE,
related_name="address",
primary_key=True,
)
class M2mWithO2oPk(Model):
name = fields.CharField(max_length=64)
address: fields.ManyToManyRelation["Address"] = fields.ManyToManyField("models.Address")
class O2oPkModelWithM2m(Model):
author: fields.OneToOneRelation[Author] = fields.OneToOneField(
"models.Author",
on_delete=fields.CASCADE,
primary_key=True,
)
nodes: fields.ManyToManyRelation["Node"] = fields.ManyToManyField("models.Node")
class Dest_null(Model):
name = fields.CharField(max_length=64)
class O2O_null(Model):
name = fields.CharField(max_length=64)
event: fields.OneToOneNullableRelation[Event] = fields.OneToOneField(
"models.Dest_null",
on_delete=fields.CASCADE,
related_name="address_null",
null=True,
)
class Team(Model):
"""
Team that is a playing
"""
id = fields.IntField(primary_key=True)
name = fields.TextField()
events: fields.ManyToManyRelation[Event]
minrelation_through: fields.ManyToManyRelation["MinRelation"]
alias = fields.IntField(null=True)
class Meta:
ordering = ["id"]
class PydanticMeta:
exclude = ("minrelations",)
def __str__(self):
return self.name
class EventTwo(Model):
id = fields.IntField(primary_key=True)
name = fields.TextField()
tournament_id = fields.IntField()
# Here we make link to events.Team, not models.Team
participants: fields.ManyToManyRelation["TeamTwo"] = fields.ManyToManyField("events.TeamTwo")
class Meta:
app = "events"
def __str__(self):
return self.name
class TeamTwo(Model):
id = fields.IntField(primary_key=True)
name = fields.TextField()
eventtwo_through: fields.ManyToManyRelation[EventTwo]
class Meta:
app = "events"
def __str__(self):
return self.name
class IntFields(Model):
id = fields.IntField(primary_key=True)
intnum = fields.IntField()
intnum_null = fields.IntField(null=True)
class BigIntFields(Model):
id = fields.BigIntField(primary_key=True)
intnum = fields.BigIntField()
intnum_null = fields.BigIntField(null=True)
class SmallIntFields(Model):
id = fields.IntField(primary_key=True)
smallintnum = fields.SmallIntField()
smallintnum_null = fields.SmallIntField(null=True)
class CharFields(Model):
id = fields.IntField(primary_key=True)
char = fields.CharField(max_length=255)
char_null = fields.CharField(max_length=255, null=True)
class TextFields(Model):
id = fields.IntField(primary_key=True)
text = fields.TextField()
text_null = fields.TextField(null=True)
class BooleanFields(Model):
id = fields.IntField(primary_key=True)
boolean = fields.BooleanField()
boolean_null = fields.BooleanField(null=True)
class BinaryFields(Model):
id = fields.IntField(primary_key=True)
binary = fields.BinaryField()
binary_null = fields.BinaryField(null=True)
class DecimalFields(Model):
id = fields.IntField(primary_key=True)
decimal = fields.DecimalField(max_digits=18, decimal_places=4)
decimal_nodec = fields.DecimalField(max_digits=18, decimal_places=0)
decimal_null = fields.DecimalField(max_digits=18, decimal_places=4, null=True)
class DatetimeFields(Model):
id = fields.IntField(primary_key=True)
datetime = fields.DatetimeField()
datetime_null = fields.DatetimeField(null=True)
datetime_auto = fields.DatetimeField(auto_now=True)
datetime_add = fields.DatetimeField(auto_now_add=True)
class TimeDeltaFields(Model):
id = fields.IntField(primary_key=True)
timedelta = fields.TimeDeltaField()
timedelta_null = fields.TimeDeltaField(null=True)
class DateFields(Model):
id = fields.IntField(primary_key=True)
date = fields.DateField()
date_null = fields.DateField(null=True)
class TimeFields(Model):
id = fields.IntField(primary_key=True)
time = fields.TimeField()
time_null = fields.TimeField(null=True)
class FloatFields(Model):
id = fields.IntField(primary_key=True)
floatnum = fields.FloatField()
floatnum_null = fields.FloatField(null=True)
def raise_if_not_dict_or_list(value: Union[dict, list]):
if not isinstance(value, (dict, list)):
raise ValidationError("Value must be a dict or list.")
class JSONFields(Model):
"""
This model contains many JSON blobs
"""
id = fields.IntField(primary_key=True)
data = fields.JSONField() # type: ignore # Test cases where generics are not provided
data_null = fields.JSONField[Union[dict, list]](null=True)
data_default = fields.JSONField[dict](default={"a": 1})
# From Python 3.10 onwards, validator can be defined with staticmethod
data_validate = fields.JSONField[Union[dict, list]](
null=True, validators=[raise_if_not_dict_or_list]
)
# Test cases where generics are provided and the type is a pydantic base model
data_pydantic = fields.JSONField[TestSchemaForJSONField](
default=json_pydantic_default, field_type=TestSchemaForJSONField
)
class UUIDFields(Model):
id = fields.UUIDField(primary_key=True, default=uuid.uuid1)
data = fields.UUIDField()
data_auto = fields.UUIDField(default=uuid.uuid4)
data_null = fields.UUIDField(null=True)
class MinRelation(Model):
id = fields.IntField(primary_key=True)
tournament: fields.ForeignKeyRelation[Tournament] = fields.ForeignKeyField("models.Tournament")
participants: fields.ManyToManyRelation[Team] = fields.ManyToManyField("models.Team")
class M2MOne(Model):
id = fields.IntField(primary_key=True)
name = fields.CharField(max_length=255, null=True)
two: fields.ManyToManyRelation["M2MTwo"] = fields.ManyToManyField(
"models.M2MTwo", related_name="one"
)
class M2MTwo(Model):
id = fields.IntField(primary_key=True)
name = fields.CharField(max_length=255, null=True)
one: fields.ManyToManyRelation[M2MOne]
class NoID(Model):
name = fields.CharField(max_length=255, null=True)
desc = fields.TextField(null=True)
class UniqueName(Model):
id = fields.IntField(primary_key=True)
name = fields.CharField(max_length=20, null=True, unique=True)
optional = fields.CharField(max_length=20, null=True)
other_optional = fields.CharField(max_length=20, null=True)
class UniqueTogetherFields(Model):
id = fields.IntField(primary_key=True)
first_name = fields.CharField(max_length=64)
last_name = fields.CharField(max_length=64)
class Meta:
unique_together = ("first_name", "last_name")
class UniqueTogetherFieldsWithFK(Model):
id = fields.IntField(primary_key=True)
text = fields.CharField(max_length=64)
tournament: fields.ForeignKeyRelation[Tournament] = fields.ForeignKeyField("models.Tournament")
class Meta:
unique_together = ("text", "tournament")
class ImplicitPkModel(Model):
value = fields.TextField()
class UUIDPkModel(Model):
id = fields.UUIDField(primary_key=True)
children: fields.ReverseRelation["UUIDFkRelatedModel"]
children_null: fields.ReverseRelation["UUIDFkRelatedNullModel"]
peers: fields.ManyToManyRelation["UUIDM2MRelatedModel"]
class UUIDFkRelatedModel(Model):
id = fields.UUIDField(primary_key=True)
name = fields.CharField(max_length=50, null=True)
model: fields.ForeignKeyRelation[UUIDPkModel] = fields.ForeignKeyField(
"models.UUIDPkModel", related_name="children"
)
class UUIDFkRelatedNullModel(Model):
id = fields.UUIDField(primary_key=True)
name = fields.CharField(max_length=50, null=True)
model: fields.ForeignKeyNullableRelation[UUIDPkModel] = fields.ForeignKeyField(
"models.UUIDPkModel", related_name=False, null=True
)
parent: fields.OneToOneNullableRelation[UUIDPkModel] = fields.OneToOneField(
"models.UUIDPkModel", related_name=False, null=True, on_delete=NO_ACTION
)
class UUIDM2MRelatedModel(Model):
id = fields.UUIDField(primary_key=True)
value = fields.TextField(default="test")
models: fields.ManyToManyRelation[UUIDPkModel] = fields.ManyToManyField(
"models.UUIDPkModel", related_name="peers"
)
class UUIDPkSourceModel(Model):
id = fields.UUIDField(primary_key=True, source_field="a")
class Meta:
table = "upsm"
class UUIDFkRelatedSourceModel(Model):
id = fields.UUIDField(primary_key=True, source_field="b")
name = fields.CharField(max_length=50, null=True, source_field="c")
model: fields.ForeignKeyRelation[UUIDPkSourceModel] = fields.ForeignKeyField(
"models.UUIDPkSourceModel", related_name="children", source_field="d"
)
class Meta:
table = "ufrsm"
class UUIDFkRelatedNullSourceModel(Model):
id = fields.UUIDField(primary_key=True, source_field="i")
name = fields.CharField(max_length=50, null=True, source_field="j")
model: fields.ForeignKeyNullableRelation[UUIDPkSourceModel] = fields.ForeignKeyField(
"models.UUIDPkSourceModel",
related_name="children_null",
source_field="k",
null=True,
)
class Meta:
table = "ufrnsm"
class UUIDM2MRelatedSourceModel(Model):
id = fields.UUIDField(primary_key=True, source_field="e")
value = fields.TextField(default="test", source_field="f")
models: fields.ManyToManyRelation[UUIDPkSourceModel] = fields.ManyToManyField(
"models.UUIDPkSourceModel",
related_name="peers",
forward_key="e",
backward_key="h",
)
class Meta:
table = "umrsm"
class CharPkModel(Model):
id = fields.CharField(max_length=64, primary_key=True)
class CharFkRelatedModel(Model):
model: fields.ForeignKeyRelation[CharPkModel] = fields.ForeignKeyField(
"models.CharPkModel", related_name="children"
)
class CharM2MRelatedModel(Model):
value = fields.TextField(default="test")
models: fields.ManyToManyRelation[CharPkModel] = fields.ManyToManyField(
"models.CharPkModel", related_name="peers"
)
class TimestampMixin:
created_at = fields.DatetimeField(null=True, auto_now_add=True)
modified_at = fields.DatetimeField(null=True, auto_now=True)
class NameMixin:
name = fields.CharField(40, unique=True)
class MyAbstractBaseModel(NameMixin, Model):
id = fields.IntField(primary_key=True)
class Meta:
abstract = True
class MyDerivedModel(TimestampMixin, MyAbstractBaseModel):
first_name = fields.CharField(20, null=True)
class CommentModel(Model):
class Meta:
table = "comments"
table_description = "Test Table comment"
id = fields.IntField(
primary_key=True, description="Primary key \r*/'`/*\n field for the comments"
)
message = fields.TextField(description="Comment messages entered in the blog post")
rating = fields.IntField(description="Upvotes done on the comment")
escaped_comment_field = fields.TextField(description="This column acts as it's own comment")
multiline_comment = fields.TextField(description="Some \n comment")
commented_by = fields.TextField()
class Employee(Model):
name = fields.CharField(max_length=50)
manager: fields.ForeignKeyNullableRelation["Employee"] = fields.ForeignKeyField(
"models.Employee", related_name="team_members", null=True, on_delete=NO_ACTION
)
team_members: fields.ReverseRelation["Employee"]
talks_to: fields.ManyToManyRelation["Employee"] = fields.ManyToManyField(
"models.Employee", related_name="gets_talked_to", on_delete=NO_ACTION
)
gets_talked_to: fields.ManyToManyRelation["Employee"]
def __str__(self):
return self.name
async def full_hierarchy__async_for(self, level=0):
"""
Demonstrates ``async for` to fetch relations
An async iterator will fetch the relationship on-demand.
"""
text = [
"{}{} (to: {}) (from: {})".format(
level * " ",
self,
", ".join(sorted([str(val) async for val in self.talks_to])),
", ".join(sorted([str(val) async for val in self.gets_talked_to])),
)
]
async for member in self.team_members:
text.append(await member.full_hierarchy__async_for(level + 1))
return "\n".join(text)
async def full_hierarchy__fetch_related(self, level=0):
"""
Demonstrates ``await .fetch_related`` to fetch relations
On prefetching the data, the relationship files will contain a regular list.
This is how one would get relations working on sync serialisation/templating frameworks.
"""
await self.fetch_related("team_members", "talks_to", "gets_talked_to")
text = [
"{}{} (to: {}) (from: {})".format(
level * " ",
self,
", ".join(sorted(str(val) for val in self.talks_to)),
", ".join(sorted(str(val) for val in self.gets_talked_to)),
)
]
for member in self.team_members:
text.append(await member.full_hierarchy__fetch_related(level + 1))
return "\n".join(text)
def name_length(self) -> int:
# Computes length of name
# Note that this function needs to be annotated with a return type so that pydantic
# can generate a valid schema
return len(self.name)
def team_size(self) -> int:
"""
Computes team size.
Note that this function needs to be annotated with a return type so that pydantic can
generate a valid schema.
Note that the pydantic serializer can't call async methods, but the tortoise helpers
pre-fetch relational data, so that it is available before serialization. So we don't
need to await the relation. We do however have to protect against the case where no
prefetching was done, hence catching and handling the
``tortoise.exceptions.NoValuesFetched`` exception.
"""
try:
return len(self.team_members)
except AttributeError:
return 0
def not_annotated(self):
raise NotImplementedError("Not Done")
class Meta:
ordering = ["id"]
class PydanticMeta:
computed = ["name_length", "team_size", "not_annotated"]
exclude = ["manager", "gets_talked_to"]
allow_cycles = True
max_recursion = 2
class StraightFields(Model):
eyedee = fields.IntField(primary_key=True, description="Da PK")
chars = fields.CharField(max_length=50, db_index=True, description="Some chars")
blip = fields.CharField(max_length=50, default="BLIP")
nullable = fields.CharField(max_length=50, null=True)
fk: fields.ForeignKeyNullableRelation["StraightFields"] = fields.ForeignKeyField(
"models.StraightFields",
related_name="fkrev",
null=True,
description="Tree!",
on_delete=NO_ACTION,
)
fkrev: fields.ReverseRelation["StraightFields"]
o2o: fields.OneToOneNullableRelation["StraightFields"] = fields.OneToOneField(
"models.StraightFields",
related_name="o2o_rev",
null=True,
description="Line",
on_delete=NO_ACTION,
)
o2o_rev: fields.Field
rel_to: fields.ManyToManyRelation["StraightFields"] = fields.ManyToManyField(
"models.StraightFields",
related_name="rel_from",
description="M2M to myself",
on_delete=fields.NO_ACTION,
)
rel_from: fields.ManyToManyRelation["StraightFields"]
class Meta:
unique_together = [["chars", "blip"]]
table_description = "Straight auto-mapped fields"
class SourceFields(Model):
"""
A Docstring.
"""
eyedee = fields.IntField(primary_key=True, source_field="sometable_id", description="Da PK")
# A regular comment
chars = fields.CharField(
max_length=50,
source_field="some_chars_table",
db_index=True,
description="Some chars",
)
#: A docstring comment
blip = fields.CharField(max_length=50, default="BLIP", source_field="da_blip")
nullable = fields.CharField(max_length=50, null=True, source_field="some_nullable")
fk: fields.ForeignKeyNullableRelation["SourceFields"] = fields.ForeignKeyField(
"models.SourceFields",
related_name="fkrev",
null=True,
source_field="fk_sometable",
description="Tree!",
on_delete=NO_ACTION,
)
fkrev: fields.ReverseRelation["SourceFields"]
o2o: fields.OneToOneNullableRelation["SourceFields"] = fields.OneToOneField(
"models.SourceFields",
related_name="o2o_rev",
null=True,
source_field="o2o_sometable",
description="Line",
on_delete=NO_ACTION,
)
o2o_rev: fields.Field
rel_to: fields.ManyToManyRelation["SourceFields"] = fields.ManyToManyField(
"models.SourceFields",
related_name="rel_from",
through="sometable_self",
forward_key="sts_forward",
backward_key="backward_sts",
description="M2M to myself",
on_delete=fields.NO_ACTION,
)
rel_from: fields.ManyToManyRelation["SourceFields"]
class Meta:
table = "sometable"
unique_together = [["chars", "blip"]]
table_description = "Source mapped fields"
class Service(IntEnum):
python_programming = 1
database_design = 2
system_administration = 3
class Currency(str, Enum):
HUF = "HUF"
EUR = "EUR"
USD = "USD"
class EnumFields(Model):
service: Service = fields.IntEnumField(Service)
currency: Currency = fields.CharEnumField(Currency, default=Currency.HUF)
class DoubleFK(Model):
name = fields.CharField(max_length=50)
left: fields.ForeignKeyNullableRelation["DoubleFK"] = fields.ForeignKeyField(
"models.DoubleFK", null=True, related_name="left_rel", on_delete=NO_ACTION
)
right: fields.ForeignKeyNullableRelation["DoubleFK"] = fields.ForeignKeyField(
"models.DoubleFK", null=True, related_name="right_rel", on_delete=NO_ACTION
)
class DefaultOrdered(Model):
one = fields.TextField()
second = fields.IntField()
class Meta:
ordering = ["one", "second"]
class FKToDefaultOrdered(Model):
link: fields.ForeignKeyRelation[DefaultOrdered] = fields.ForeignKeyField(
"models.DefaultOrdered", related_name="related"
)
value = fields.IntField()
class DefaultOrderedDesc(Model):
one = fields.TextField()
second = fields.IntField()
class Meta:
ordering = ["-one"]
class SourceFieldPk(Model):
id = fields.IntField(primary_key=True, source_field="counter")
name = fields.CharField(max_length=255)
class DefaultOrderedInvalid(Model):
one = fields.TextField()
second = fields.IntField()
class Meta:
ordering = ["one", "third"]
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"
)
class Principal(Model):
id = fields.IntField(primary_key=True)
name = fields.TextField()
school: fields.OneToOneRelation[School] = fields.OneToOneField(
"models.School",
on_delete=fields.CASCADE,
related_name="principal",
to_field="id",
)
class Signals(Model):
name = fields.CharField(max_length=255)
class DefaultUpdate(Model):
created_at = fields.DatetimeField(auto_now_add=True)
updated_at = fields.DatetimeField(auto_now=True)
class DefaultModel(Model):
int_default = fields.IntField(default=1)
float_default = fields.FloatField(default=1.5)
decimal_default = fields.DecimalField(max_digits=8, decimal_places=2, default=Decimal(1))
bool_default = fields.BooleanField(default=True)
char_default = fields.CharField(max_length=20, default="tortoise")
date_default = fields.DateField(default=datetime.date(year=2020, month=5, day=21))
datetime_default = fields.DatetimeField(
default=datetime.datetime(year=2020, month=5, day=20, tzinfo=pytz.utc)
)
class RequiredPKModel(Model):
id = fields.CharField(primary_key=True, max_length=100)
name = fields.CharField(max_length=255)
class ValidatorModel(Model):
regex = fields.CharField(max_length=100, null=True, validators=[RegexValidator("abc.+", re.I)])
max_length = fields.CharField(max_length=5, null=True)
ipv4 = fields.CharField(max_length=100, null=True, validators=[validate_ipv4_address])
ipv6 = fields.CharField(max_length=100, null=True, validators=[validate_ipv6_address])
max_value = fields.IntField(null=True, validators=[MaxValueValidator(20.0)])
min_value = fields.IntField(null=True, validators=[MinValueValidator(10.0)])
max_value_decimal = fields.DecimalField(
max_digits=12,
decimal_places=3,
null=True,
validators=[MaxValueValidator(Decimal("2.0"))],
)
min_value_decimal = fields.DecimalField(
max_digits=12,
decimal_places=3,
null=True,
validators=[MinValueValidator(Decimal("1.0"))],
)
comma_separated_integer_list = fields.CharField(
max_length=100, null=True, validators=[CommaSeparatedIntegerListValidator()]
)
class NumberSourceField(Model):
number = fields.IntField(source_field="counter", default=0)
class StatusQuerySet(QuerySet):
def active(self):
return self.filter(status=1)
class StatusManager(Manager):
def __init__(self, model=None, queryset_cls=None) -> None:
super().__init__(model=model)
self.queryset_cls = queryset_cls or QuerySet
def get_queryset(self):
return self.queryset_cls(self._model)
class AbstractManagerModel(Model):
all_objects = Manager()
status = fields.IntField(default=0)
class Meta:
manager = StatusManager()
abstract = True
class User(Model):
id = fields.IntField(primary_key=True)
username = fields.CharField(max_length=32)
mail = fields.CharField(max_length=64)
bio = fields.TextField()
class ManagerModel(AbstractManagerModel):
class Meta:
manager = StatusManager(queryset_cls=StatusQuerySet)
class ManagerModelExtra(AbstractManagerModel):
extra = fields.CharField(max_length=200)
class Extra(Model):
"""Dumb model, has no fk.
src: https://github.com/tortoise/tortoise-orm/pull/826#issuecomment-883341557
"""
id = fields.IntField(primary_key=True)
# currently, tortoise don't save models with single pk field for some reason \_0_/
some_name = fields.CharField(default=lambda: str(uuid.uuid4()), max_length=64)
class Single(Model):
"""Dumb model, having single fk
src: https://github.com/tortoise/tortoise-orm/pull/826#issuecomment-883341557
"""
id = fields.IntField(primary_key=True)
extra: fields.ForeignKeyNullableRelation[Extra] = fields.ForeignKeyField(
"models.Extra", related_name="singles", null=True
)
class Pair(Model):
"""Dumb model, having double fk
src: https://github.com/tortoise/tortoise-orm/pull/826#issuecomment-883341557
"""
id = fields.IntField(primary_key=True)
left: fields.ForeignKeyNullableRelation[Single] = fields.ForeignKeyField(
"models.Single", related_name="lefts", null=True
)
right: fields.ForeignKeyNullableRelation[Single] = fields.ForeignKeyField(
"models.Single", related_name="rights", null=True, on_delete=NO_ACTION
)
class OldStyleModel(Model):
id = fields.IntField(pk=True)
external_id = fields.IntField(index=True)
def camelize_var(var_name: str):
var_parts: List[str] = var_name.split("_")
return var_parts[0] + "".join([part.title() for part in var_parts[1:]])
class CamelCaseAliasPerson(Model):
"""CamelCaseAliasPerson model.
- A model that generates camelized aliases automatically by
configuring config_class.
"""
id = fields.IntField(primary_key=True)
first_name = fields.CharField(max_length=255)
last_name = fields.CharField(max_length=255)
full_address = fields.TextField(null=True)
class PydanticMeta:
"""Defines the default config for pydantic model generator."""
model_config = ConfigDict(
title="My custom title",
extra="ignore",
alias_generator=camelize_var,
populate_by_name=True,
)
def callable_default() -> str:
return "callable_default"
async def async_callable_default() -> str:
return "async_callable_default"
class CallableDefault(Model):
id = fields.IntField(primary_key=True)
callable_default = fields.CharField(max_length=32, default=callable_default)
async_default = fields.CharField(max_length=32, default=async_callable_default)