All posts

Piccolo column choices

A new feature was recently added to Piccolo, which allows choices to be specified for columns. It leverages Python's Enum support. Here's an example:

from enum import Enum

from piccolo.columns import Varchar
from piccolo.table import Table


class Director(Table):
    class Gender(str, Enum):
        male = 'm'
        female = 'f'
        non_binary = 'n'

    name = Varchar(length=100)
    gender = Varchar(length=1, choices=Gender)

You can now do queries like this:

>>> Director.select().where(
>>>     Director.gender == Director.Gender.male
>>> ).run_sync()
[{'id': 1, 'name': 'George Lucas', 'gender': 'm'}, ...]

>>> director = Director(
>>>     name="Brenda Barton",
>>>     gender=Director.Gender.female
>>> )
>>> director.save().run_sync()

Piccolo Admin also supports this feature. When a column has choices specified, a select widget is rendered in the UI.

Column choices UI

Posted on: 29 May 2021

Have any comments or feedback on this post? Chat with us on GitHub.