Home > front end >  Django ChoiceField and RadioSelect return EnumMeta error
Django ChoiceField and RadioSelect return EnumMeta error

Time:12-06

I have a ChoiceField that I would like to show as Radio selection but I get the following error:

EnumMeta.call() missing 1 required positional argument: 'value'

Here it is the code

model.py

class Answer(models.IntegerChoices):
    NO = 0, _('No')
    YES = 1, _('Yes')

form.py

    question = forms.ChoiceField(
        choices=Answer,
        widget=forms.RadioSelect()
    )

I've followed the django documentation and I can't figure it out the nature of the error, thank you for any explanation that you can give me

CodePudding user response:

You can not pass an IntegerChoices class as choices=… parameter. However an IntegerChoices class makes it more convenient to define choices, you can use the .choices attribute so:

question = forms.ChoiceField(
    choices=Answer.choices,
    widget=forms.RadioSelect()
)
  • Related