Django ModelForm选择类型表单,其中empty_label不显示在Django中

时间:2018-05-12 05:36:23

标签: django django-forms

这里是我的forms.py

class EmployeeForm(forms.ModelForm):

    class Meta:
        model = Employee
        fields = '__all__'
        labels = {'company': 'Company ', }
        empty_labels = {'company': 'Select Company ', }

class InventoryForm(forms.ModelForm):
    name = forms.CharField(label='Nama ', max_length=255)
    employee = forms.ModelChoiceField(
        label='Employee ',
        empty_label="Select Employee",
        to_field_name="id",
        queryset=Employee.objects.all(),
        required=False,
        )

    class Meta:
        model = Inventory
        fields = '__all__'

在我的代码上,两个班都试图为我的empty_label类型表单设置select

但在EmployeeForm上没有显示。 empty_label上没有class Meta?...

如果想要使用class Metaemply_label类似二等,我该怎么办。

1 个答案:

答案 0 :(得分:0)

您必须明确定义company中的EmployeeForm字段,同时保留Meta类,就像您在InventoryForm中所做的那样:

class EmployeeForm(forms.ModelForm):

    employee = forms.ModelChoiceField(
        label='Employee ',
        empty_label="Select Employee",
        to_field_name="id",
        queryset=Employee.objects.all(),
        required=False,
    )        
    class Meta:
        model = Employee
        fields = '__all__'

小工具,标签等可以在widgets = {...}课程中设置为labels = {...}Meta