如何使用empty_lable保存ForeignKey的ModelChoiceField? - Django

时间:2018-06-15 03:17:18

标签: django python-3.x foreign-keys modelchoicefield

我想保存SelfTaught('自学'作为附图)的值,但是它提醒选择列表中的项目,我有模型和表单的部分代码如下:

class Course(models.Model):
    teacher = models.ForeignKey(
        User,
        on_delete=models.CASCADE, null=True,
        related_name="+"
    )

teacher = forms.ModelChoiceField(queryset=User.objects.filter(is_teacher=True),
                                empty_label='SeltTaught',label='Teacher' )

1 个答案:

答案 0 :(得分:0)

尝试添加required=False,如下所示,默认为True


teacher = forms.ModelChoiceField(queryset=User.objects.filter(is_teacher=True),
                                empty_label='SeltTaught',label='Teacher',required=True)