Dicky Raambo
Dicky Raambo

Reputation: 523

Django ModelForm select type form with empty_label not Showing in Django

here my 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__'

on my code, both class trying to set empty_label for my select type form.

but on EmployeeForm it is not showing. there no empty_label on class Meta?...

how should I do if want use class Meta with emply_label like second class.

Upvotes: 1

Views: 474

Answers (1)

codeandfire
codeandfire

Reputation: 465

You will have to explicitly define the company field in EmployeeForm, while keeping the Meta class, just like you have done in 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 etc. can be set as widgets = {...} and labels = {...} in the Meta class.

Upvotes: 1

Related Questions