Opened 4 years ago

Closed 4 years ago

#31820 closed Bug (invalid)

CheckboxSelectMultiple widget doesn't work with TextChoices enabled CharFields

Reported by: Remy Owned by: nobody
Component: Forms Version: 3.0
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Remy)

# 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."

Change History (2)

comment:1 by Remy, 4 years ago

Description: modified (diff)

comment:2 by Mariusz Felisiak, 4 years ago

Resolution: invalid
Status: newclosed

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 support channels.

Note: See TracTickets for help on using tickets.
Back to Top