Add a placeholder to a form based on a model charfield

I have a form:

class SideEffectForm(ModelForm):

    class Meta:
        model = SideEffect
        fields = ['se_name']

    def __init__(self, *args, p, **kwargs):
        super().__init__(*args, **kwargs)

        if p == 'antipsychotic':

            self.fields['se_name'].choices = [
                ("insomnia_and_agitation", "Insomnida and Agitation"),
                ("akathisia", "Akathisia"),
                ("dystonia", "Dystonia"),
            ]

That is based on this model:

class SideEffect(TimeStampedModel):

    SE_CHOICES = [
        ("insomnia_and_agitation", "Insomnida and Agitation"),
        ("akathisia", "Akathisia"),
        ("anticholinergic_effects", "Anticholinergic Side Effects")
    ]

    se_name = models.CharField("",max_length=200, choices=SE_CHOICES, blank=False)

    concern = models.IntegerField("",default=50)

    case = models.ForeignKey(Case, on_delete=models.CASCADE)

I have a form:

class SideEffectForm(ModelForm):

    class Meta:
        model = SideEffect
        fields = ['se_name']

    def __init__(self, *args, p, **kwargs):
        super().__init__(*args, **kwargs)

        if p == 'antipsychotic':

            self.fields['se_name'].choices = [
                ("insomnia_and_agitation", "Insomnida and Agitation"),
                ("akathisia", "Akathisia"),
                ("dystonia", "Dystonia"),
            ]

That is based on this model:

class SideEffect(TimeStampedModel):

    SE_CHOICES = [
        ("insomnia_and_agitation", "Insomnida and Agitation"),
        ("akathisia", "Akathisia"),
        ("anticholinergic_effects", "Anticholinergic Side Effects")
    ]

    se_name = models.CharField("",max_length=200, choices=SE_CHOICES, blank=False)

    concern = models.IntegerField("",default=50)

    case = models.ForeignKey(Case, on_delete=models.CASCADE)

As it stands the form displays the first option. I would however like to have a place holder e.g. ‘Please select a side effect’. I could do this by having it as one of the options but would prefer not to as then would need to implement measures to stop the placeholder being saved as a valid user entry. Is there a way to specify a placeholder?

The HTML select element does not provide for such a feature.

You do have at least two options:

  • See the last paragraph in the section for Field choices

  • Find a usable JavaScript-based select widget that provides that functionality.

1 Like

That’s totally possible.

Check out the docs at Model field reference | Django documentation | Django

Unless blank=False is set on the field along with a default then a label containing "---------" will be rendered with the select box. To override this behavior, add a tuple to choices containing None ; e.g. (None, 'Your String For Display') . Alternatively, you can use an empty string instead of None where this makes sense - such as on a CharField.

And you already set the se_name field as blank=False. So you need to add a (None, "Display Value") to your SE_CHOICES

Thanks @leandrodesouzadev might you then also be able to advise how i ensure the user cannot submit the form while the (None, 'Your String For Display') option is selected (i.e. force them to make a choice before submitting the form)

Thanks!

Since you want that to happen in the browser, you would need to write some JavaScript that handled the submit button to validate that field before processing the submit.

1 Like