My goal is to be able to use get_FOO_display(), and so as far as I understand I will have to have choices specified in the model field. At the same time I am wanting to render the form using a ModelForm as a RadioButton.
The problem I am having is that the default value of “——” that would be used in a dropdown select is showing up as one of my RadioButton options.
models.py
class Medication(models.Model):
YESNO_CHOICES = [(0, 'No'), (1, 'Yes')]
Allergies = models.BigIntegerField(verbose_name='Allergies:', choices=YESNO_CHOICES)
forms.py
I have tried just specifying a RadioButton widget in the ModelForm.
class mfMedication(ModelForm):
class Meta:
model = Medication
widgets = {
'Allergies': RadioSelect(),
}
and also specifying the RadioButton with the CHOICES.
class mfMedication(ModelForm):
class Meta:
model = Medication
widgets = {
'Allergies': RadioSelect(choices=Medication.YESNO_CHOICES),
}
In both cases I get three radiobuttons:
"": -------
0 : No
1 : Yes
The only way I do not get the “——-” is to remove choices=YESNO_CHOICES from my model field, but this stops get_FOO_display() from working.
Any approach that you have used to get this working would be greatly appreciated.
Thanks. JD.
If you want to prevent displaying of
-------choice, you should specifyempty_label=Nonein your form field.Also, I recomend you to use BooleanField for model and TypedChoiceField for form:
models.py:
forms.py: