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:
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…
class MyModel(models.TextChoices):
YES = '', ('Yes')
NO = 'No', ('No')
I_DONT_KNOW = 'I don\'t know', ('I don\'t know')
happy_field = models.CharField(
('Are you happy?'),
choices=MyModel.choices,
default=False,
blank=True
)
default=False + a blank value for one of the choices make it appear correct (= with no “------”) but If the submitter select “Yes”, there’s a validation error.
Edit: the validation error appears when the user select the choice with a blank value (here, “Yes”). The error is “Choose a valid Choice. False isn’t one.” (translated from french)
Should I move the choice class to forms.py and add a ChoiceField directly there?
I believe (but am not sure) that the issue with the null value is that the page isn’t going to return a value for that field from the form. (That’s not a “blank” field, it’s a null field - there isn’t any space between the quotes - not that that makes a difference in this specific case.)
Anyway, without having a chance to look at this myself, my first guess would be to mark that field as “required=False” in the form (not the model).
Thank you for you suggestion.
I believe I’ve just solved the issue by deciding it’s not one
Fields marked as blank=True in models.py reflect their non-mandatory nature in the survey concepters mind. But as I’ve set their widgets as being radio buttons, the submitter actually need a possibility to change their mind if they first check a button and later decide not to answer. Seems to a pretty good reason to leave the “------” to me.
It didn’t occur to me this way at first, but here again, stating things out loud helped me figure it out. Thanks!
Getting back to this, I realize that I actually need to get rid of the "---------" when ForeignKey or TextChoices related fields are marked as blank=True.
The django doc paragraph indicated by Ken on the second post of this thread suggests to add a tuple to the choices containing None. That would work when the form loads a Select widget for this field, but I’d like to use a RadioSelect widget. See the screenshots on the first post of the thread to see what I’m looking for precisely.