shaare.it

ValidationError: invalid choice for form field

7 Dec 2025

1 min read

ValidationError: invalid choice for form field

$ python manage.py shell -c "from django import forms; class F(forms.Form): x=forms.ChoiceField(choices=[('a','A')]); F({'x':'b'}).is_valid()"
False

Why this happens

Value not in choices.

Fix

Add choice or validate input to allowed values.

Wrong code

F({"x": "b"}).is_valid()

Fixed code

F({"x": "a"}).is_valid()
# or expand choices