如何添加新群组,但教师必须来自列表

时间:2017-01-12 14:30:36

标签: python django

我想添加一个新组,但是对于教师,他们必须从教师表中的列表中进行选择,因为它与组有关系。

表格:老师,小组
关系:教师 1到多

models.py

class Teacher(models.Model):
    name = models.CharField(max_length=100)
    def __str__(self):
        return '%s' % (self.name)

class Group(models.Model):
    name = models.CharField(max_length=100)
    teacher = models.ForeignKey(Leraren, on_delete=models.CASCADE)
    def __str__(self):
        return '%s %s' % (self.name, self.teacher)

forms.py

class TeacherForm(forms.Form):
    name = forms.CharField(required=True, label='name', max_length=100)

class GroupForm(forms.Form):
    name = forms.CharField(required=True, label='name', max_length=100)
    teacher = # choose from list of teachers that are in the db Teachers #

1 个答案:

答案 0 :(得分:1)

只需将表单定义为ModelForm的{​​{1}},即无需手动执行此操作

Group