我有一个课程目录,并试图包括预先获得。 课程有0到n个预先录取,课程可以是0到n个课程的前提。 课程类如下
class Course(models.Model):
class Meta:
ordering = ['course_code', 'title']
course_id = models.AutoField(primary_key=True)
course_code = models.SlugField(max_length=20, null=False, blank=False)
....
....
class Pre_requisit(models.Model)
course = models.ForeignKey(Course, course_id')
pre_req = ???????
我已经尝试将pre_req字段作为ForeginKey和MamyToManyField,但无法找到解决方案。 Django不允许2个来自Pre_requisit类的ForeignKeys到Course类。 随着ManyToMany字段逐渐通过另一个表格,我仍然会遇到错误。
任何人都可以让我知道如何实现这种关系。 我希望确保预先获得的课程存在,并能够链接到它,以便它可以显示。
非常感谢。
答案 0 :(得分:0)
class Course(models.Model):
....
pre_req = models.ManyToManyField("self")
在http://docs.djangoproject.com/en/dev/ref/models/fields/#manytomanyfield
了解详情如果您希望存储额外信息,您还可以指定“通过”表格(http://docs.djangoproject.com/en/dev/topics/db/models/#intermediary-manytomany处的信息),例如硬预先要求或推荐等。