Django ManyToManyField连接到另一个模型中的字段。 (项目管理)

时间:2019-06-08 17:45:24

标签: django django-models django-rest-framework django-templates django-views

我有一个与成员一起参加的SchoolClass。我现在想创建一个连接到某些成员的项目。我该怎么办?

class SchoolKlass(models.Model):
    name = models.CharField(max_length=50)
    members = models.ManyToManyField(UserProfile, blank=True,related_name='a')

    class Meta:
        verbose_name_plural = 'Klasser'

    def __str__(self):
        return self.user.username

class Project(models.Model):
    members = models.ManyToManyField(SchoolKlass.members, blank=True)
    name = models.CharField(max_length=200)
    description = models.CharField(max_length=1000)
    schoolKlass = models.ForeignKey(SchoolKlass, on_delete=models.CASCADE)

如您所见,我想访问Project模型的许多关系中的SchoolKlass.members。

1 个答案:

答案 0 :(得分:0)

用户通报导入..

class SchoolKlass(models.Model):
name = models.CharField(max_length=50)
members = models.ManyToManyField(UserProfile, blank=True,related_name='a')

class Meta:
    verbose_name_plural = 'Klasser'

    def __str__(self):
        return self.user.username

class Project(models.Model):
    members = models.ManyToManyField("SchoolKlass.members", blank=True)
    name = models.CharField(max_length=200)
    description = models.CharField(max_length=1000)
    schoolKlass = models.ForeignKey(SchoolKlass, on_delete=models.CASCADE)

在已提交的关系上提供“ appname.model”。别忘了报价