[Django] #31820: CheckboxSelectMultiple widget doesn't work with TextChoices enabled CharFields

37 views
Skip to first unread message

Django

unread,
Jul 23, 2020, 5:33:03 AM7/23/20
to django-...@googlegroups.com
#31820: CheckboxSelectMultiple widget doesn't work with TextChoices enabled
CharFields
--------------------------------------+------------------------
Reporter: Remy | Owner: nobody
Type: Bug | Status: new
Component: Forms | Version: 3.0
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
--------------------------------------+------------------------
# 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>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jul 23, 2020, 5:56:44 AM7/23/20
to django-...@googlegroups.com
#31820: CheckboxSelectMultiple widget doesn't work with TextChoices enabled
CharFields
------------------------+--------------------------------------

Reporter: Remy | Owner: nobody
Type: Bug | Status: new
Component: Forms | Version: 3.0
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------+--------------------------------------
Description changed by Remy:

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>

Django

unread,
Jul 24, 2020, 1:11:29 AM7/24/20
to django-...@googlegroups.com
#31820: CheckboxSelectMultiple widget doesn't work with TextChoices enabled
CharFields
------------------------+--------------------------------------
Reporter: Remy | Owner: nobody
Type: Bug | Status: closed
Component: Forms | Version: 3.0
Severity: Normal | Resolution: invalid

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------+--------------------------------------
Changes (by felixxm):

* 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>

Reply all
Reply to author
Forward
0 new messages