还没有测试过这个,但是基于读取Django的代码
here
和
here
我认为它应该起作用:
class ThingForm(models.ModelForm):
class Meta:
model = Thing
def __init__(self, *args, **kwargs):
super(ThingForm, self).__init__(*args, **kwargs)
self.fields['verb'].empty_label = None
编辑
这是
documented
但是,如果使用自动生成的模型窗体,则不一定知道要查找ModelChoiceField。
编辑
:正如jlpp在他的回答中指出的,这并不完整-您必须在更改空的_label属性后将选项重新分配给小部件。由于这有点老土,另一个可能更容易理解的选项就是覆盖整个ModelChoiceField:
class ThingForm(models.ModelForm):
verb = ModelChoiceField(Verb.objects.all(), empty_label=None)
class Meta:
model = Thing