Edit form Select field not shows the empty label or not show the selected value in DJango

I have a ModelForm name ProductForm with model Product, many to one relation with Type. So automatically this form creates me a Select for select the Type of Product. Then if I set all data in ModelForm according with the docs in Django, everything works well, but i would like add an emtpy label to the Select. Like this:

class Product(models.Model):
    type= models.ForeignKey(Types, on_delete=models.CASCADE)
    code=models.CharField(max_length=50)
    name=models.CharField(max_length=1000)
    details=models.CharField(max_length=1000)
    um=models.CharField(max_length=50)
    def __str__(self):
        return self.name

class ProductForm(ModelForm):    
    class Meta:
        model = Product
        fields='__all__'
        labels = {
            'code': 'Code',
            'name': 'Product',
            'type':'Product type',
            'um': 'UM',
            'details': 'Details'
        }
        widgets = {
            'code': TextInput(
                attrs={
                    'class':'form-control',
                    'placeholder':'Code'
                }
            ),
            'name': TextInput(
                attrs={
                    'class':'form-control',
                    'placeholder':'Product'
                }
            ),
            'um': TextInput(
                attrs={
                    'class':'form-control',
                    'placeholder':'UM'
                }
            ),
            'type' : Select(attrs={'style':'width: 100%', 'class':'form-control select2bs4'}),
            'details': Textarea(
                attrs={
                    'style':'height:150px',
                    'class':'form-control',
                    'placeholder':'Details'
                }
            ),
        }

But with this I have no idea where put the empty_label for the Select, because emtpy_label is for ModelChoise. So, I use another solution, changing the ModelForm like bellow (only that code, not the code above), works the empty label, but the edit form, not shows the selected value, So is one or other, but i like both of them:

class ProductForm(ModelForm):
    code= CharField(label='Code',widget=TextInput(attrs={'class':'form-control','placeholder':'Code'}))
    name = CharField(label='Product',widget=TextInput(attrs={'class':'form-control', 'placeholder':'Product'}))
    um = CharField(label='UM',widget=TextInput(attrs={'class':'form-control', 'placeholder':'UM'}))
    type= ModelChoiceField(label='Product Type',queryset=Types.objects.all(), empty_label='Please select one: ', widget=Select(attrs={'style':'width: 100%', 'class':'form-control select2bs4'}), to_field_name='type')
    details = CharField(label='Details',widget=Textarea(attrs={'style':'height:150px', 'class':'form-control', 'placeholder':'Details'}))
class Meta:
        model = Product
        fields='__all__'

I just eliminate the , to_field_name='type' and works!!!!

You can do de following:

from django import forms

from core import models


class ProductForm(forms.ModelForm):

    class Meta:
        model = models.Product
        fields='__all__'

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['type'].label = 'Product type'
        self.fields['type'].empty_label = 'Select a product type'
        self.fields['type'].widget.attrs.update({
            'style':'width: 100%',
            'class':'form-control select2bs4'
        })

This is the result:

Screenshot of a select with custom empty label

Back to Top