class MovieForm(forms.ModelForm):
class Meta:
model = Movie
fields = [
'genres',
]
widgets = {
'genres': forms.CheckboxSelectMultiple
}
}}}
# models.py
{{{
class Movie(models.Model):
class Genre(models.TextChoices):
SCIFI = 'S', 'Science Fiction'
ACTION = 'A', 'Action'
genres = models.charField(max_length=1, choices=Genre.choices)
}}}
# template
{{{
{{ form.genres.errors }}
}}}
Select one checkbox for example, then post the form. On `form.save()`, the
following error is returned to the template:
{{{
"Select a valid choice. ['S'] is not one of the available choices."
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/31820>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
Old description:
> # forms.py
> {{{
>
> class MovieForm(forms.ModelForm):
>
> class Meta:
> model = Movie
> fields = [
> 'genres',
> ]
> widgets = {
> 'genres': forms.CheckboxSelectMultiple
> }
> }}}
>
> # models.py
> {{{
> class Movie(models.Model):
> class Genre(models.TextChoices):
> SCIFI = 'S', 'Science Fiction'
> ACTION = 'A', 'Action'
>
> genres = models.charField(max_length=1, choices=Genre.choices)
> }}}
>
> # template
> {{{
> {{ form.genres.errors }}
> }}}
>
> Select one checkbox for example, then post the form. On `form.save()`,
> the following error is returned to the template:
> {{{
> "Select a valid choice. ['S'] is not one of the available choices."
> }}}
New description:
# forms.py
{{{
class MovieForm(forms.ModelForm):
class Meta:
model = Movie
fields = [
'genres',
]
widgets = {
'genres': forms.CheckboxSelectMultiple
}
}}}
# models.py
{{{
class Movie(models.Model):
class Genre(models.TextChoices):
SCIFI = 'S', 'Science Fiction'
ACTION = 'A', 'Action'
genres = models.CharField(max_length=1, choices=Genre.choices)
}}}
# template
{{{
{{ form.genres.errors }}
}}}
Select one checkbox for example, then post the form. On `form.save()`, the
following error is returned to the template:
{{{
"Select a valid choice. ['S'] is not one of the available choices."
}}}
--
--
Ticket URL: <https://code.djangoproject.com/ticket/31820#comment:1>
* status: new => closed
* resolution: => invalid
Comment:
It's not related with `TextChoices`, it will not work also with
`choices=[('S', 'Science Fiction'), ('A', 'Action')]` because `CharField`
cannot handle multiple values. Please use one of
[https://code.djangoproject.com/wiki/TicketClosingReasons/UseSupportChannels
support channels].
--
Ticket URL: <https://code.djangoproject.com/ticket/31820#comment:2>