How to hide the default placeholder on a form

Hi,

I’m working on a ModelForm. When a field is set to blank=False in models.py, the form element appears like this:

This screenshot corresponds to this in models.py:

field1 = models.ForeignKey(
    'SomeModel',
    on_delete=models.CASCADE,
    verbose_name='some verbose name',
    related_name='some_related_name',
    )

Which is how I want it to appear. However, when the model indicates blank=True (which is needed), I have this kind of placeholder default (the “---------”) that I can’t get rid of:

In models.py:

    field2 = models.CharField(
        _('Some label'),
        choices=Some.choices,
        max_length=100,
        blank=True
        )

Is it possible to get the appearance of the first image on a filed where blank is set to True?

See the paragraph immediately before this link - Model field reference | Django documentation | Django

Great, thank you very much Ken!

It works great when dealing with choices written in the models.py file. Would you know if there’s a chance it works when dealing with a ForeignKey reffering to a CharField?

I tried to set the field to null=True, then add a new line in the database table and set it either to null or to an empty content, but without luck so far…

See the empty_label attribute for the ModelChoiceField.

1 Like

Perfect. Thanks again!