在类中调用同一类的实例

我在Django中有一个名为Student的模型,我希望一个可变的可变的学生成为与他们成为朋友的其他学生。例如:

Tim.friends_set.all()=吉姆,鲍勃,萨姆

我的______在这段代码中是什么?有没有一种方法可以在不创建新模型的情况下进行操作?

class Student(models.Model):
    name = models.CharField(max_length=128, unique=True)
    friends = models.________(Student, blank = True)

如果我创建了一个新模型,我会想像一下:

class friends(models.Model):
    student = models.ForeignKey(Student)
    friends = models.models.ManyToManyField(Student, blank = True)
丹尼尔·B

ManyToManyField与参数一起使用'self'

class Student(models.Model):
  name = models.CharField(max_length=128, unique=True)
  friends = models.ManyToManyField('self', blank = True)

您不妨考虑一下这种关系是否对称

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章