@@ -83,3 +83,59 @@ use mixins to reduce the amount of repetition.
8383
8484 class Manager (FavouriteMixin , Table ):
8585 name = Varchar()
86+
87+ -------------------------------------------------------------------------------
88+
89+ Choices
90+ -------
91+
92+ You can specify choices for a column, using Python's ``Enum `` support.
93+
94+ .. code-block :: python
95+
96+ from enum import Enum
97+
98+ from piccolo.columns import Varchar
99+ from piccolo.table import Table
100+
101+
102+ class Shirt (Table ):
103+ class Size (str , Enum ):
104+ small = ' s'
105+ medium = ' m'
106+ large = ' l'
107+
108+ size = Varchar(length = 1 , choices = Size)
109+
110+ We can then use the ``Enum `` in our queries.
111+
112+ .. code-block :: python
113+
114+ >> > Shirt(size = Shirt.Size.large).save().run_sync()
115+
116+ >> > Shirt.select().run_sync()
117+ [{' id' : 1 , ' size' : ' l' }]
118+
119+ Note how the value stored in the database is the ``Enum `` value (in this case ``'l' ``).
120+
121+ You can also use the ``Enum `` in ``where `` clauses, and in most other situations
122+ where a query requires a value.
123+
124+ .. code-block :: python
125+
126+ >> > Shirt.insert(
127+ >> > Shirt(size = Shirt.Size.small),
128+ >> > Shirt(size = Shirt.Size.medium)
129+ >> > ).run_sync()
130+
131+ >> > Shirt.select().where(Shirt.size == Shirt.Size.small).run_sync()
132+ [{' id' : 1 , ' size' : ' s' }]
133+
134+ Advantages
135+ ~~~~~~~~~~
136+
137+ By using choices, you get the following benefits:
138+
139+ * Signalling to other programmers what values are acceptable for the column.
140+ * Improved storage efficiency (we can store ``'l' `` instead of ``'large' ``).
141+ * Piccolo admin support (in progress)
0 commit comments