[Django]-Specify choices in model and RadioButton in ModelForm renders not selected value "β€”β€”"

9πŸ‘

βœ…

If you want to prevent displaying of ------- choice, you should specify empty_label=None in your form field.

Also, I recomend you to use BooleanField for model and TypedChoiceField for form:

models.py:

class Medication(models.Model):
    Allergies = models.BooleanField('Allergies:')

forms.py:

class MedicationForm(forms.ModelForm):
    YESNO_CHOICES = ((0, 'No'), (1, 'Yes'))
    Allergies = forms.TypedChoiceField(
                     choices=YESNO_CHOICES, widget=forms.RadioSelect, coerce=int
                )

1πŸ‘

Using the BigIntegerField, you can also set the default=0 or whatever choice you would like it to be.

πŸ‘€imapotatoe123

Leave a comment