Skip to content

Support dict for SelectForm.choices #886

@marksteward

Description

@marksteward

A list of tuples made sense before dicts were ordered by default, but we're now already using dicts for optgroup support. It'd be nice to be able to do:

class PastebinEntry(Form):
    language = SelectField(
        'Programming Language',
        choices={
            'cpp': 'C++',
            'py': 'Python',
            'text': 'Plain Text',
        },
    )

form = PastebinEntry()
del form.choices['cpp']  # instead of having to iterate over .choices and filter out 'cpp'

This could still coexist with an optgroup version:

class PastebinEntry(Form):
    language = SelectField(
        'Programming Language',
        choices={
            'Actual languages': {'cpp': 'C++', 'py': 'Python'},
            'Generic': {'text': 'Plain Text'},
        },
    )

or the current allowed format:

class PastebinEntry(Form):
    language = SelectField(
        'Programming Language',
        choices={
            'Actual languages': [('cpp', 'C++'), ('py', 'Python')],
            'Generic': [('text', 'Plain Text')],
        },
    )

Happy to provide a PR if this sounds sensible!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions