首先我们来熟悉一下Django Model中外键的定义方式:
class ForeignKey(to, on_delete, **options)
A many-to-one relationship. Requires two positional arguments: the class to which the model is related and the on_delete option.
对于多对一的关系定义,我们需要传递两个位参,一个是要参照的模型,一个是on_delete选项
To create a recursive relationship – an object that has a many-to-one relationship with itself – use models.ForeignKey(‘self’, on_delete=models.CASCADE).
如果想要创建一个递归的关系参照,比如以自己来创建多对一关系,使用models.ForeignKey('self', on_delete=models.CASCADE)
If you need to create a relationship on a model that has not yet been defined, you can use the name of the model, rather than the model object itself:
from django.db import models
<