@@ -306,6 +306,58 @@ Sync usage requires that you call `fetch_related` before the time, and then you
306
306
307
307
The reverse lookup of ``team.event_team `` works exactly the same way.
308
308
309
+ Improving relational type hinting
310
+ =================================
311
+
312
+ Since Tortoise ORM is still a young project, it does not have such widespread support by
313
+ various editors who help you writing code using good autocomplete for models and
314
+ different relations between them.
315
+ However, you can get such autocomplete by doing a little work yourself.
316
+ All you need to do is add a few annotations to your models for fields that are responsible
317
+ for the relations.
318
+
319
+ Here is an updated example from :ref: `getting_started `, that will add autocomplete for
320
+ all models including fields for the relations between models.
321
+
322
+ .. code-block :: python3
323
+
324
+ from tortoise.models import Model
325
+ from tortoise import fields
326
+
327
+
328
+ class Tournament(Model):
329
+ id = fields.IntField(pk=True)
330
+ name = fields.CharField(max_length=255)
331
+
332
+ events: fields.ReverseRelation["Event"]
333
+
334
+ def __str__(self):
335
+ return self.name
336
+
337
+
338
+ class Event(Model):
339
+ id = fields.IntField(pk=True)
340
+ name = fields.CharField(max_length=255)
341
+ tournament: fields.ForeignKeyRelation[Tournament] = fields.ForeignKeyField(
342
+ "models.Tournament", related_name="events"
343
+ )
344
+ participants: fields.ManyToManyRelation["Team"] = fields.ManyToManyField(
345
+ "models.Team", related_name="events", through="event_team"
346
+ )
347
+
348
+ def __str__(self):
349
+ return self.name
350
+
351
+
352
+ class Team(Model):
353
+ id = fields.IntField(pk=True)
354
+ name = fields.CharField(max_length=255)
355
+
356
+ events: fields.ManyToManyRelation[Event]
357
+
358
+ def __str__(self):
359
+ return self.name
360
+
309
361
310
362
Reference
311
363
=========
0 commit comments