Specifying a mySQL ENUM in a Django model

Steve picture Steve · Aug 22, 2008 · Viewed 57.6k times · Source

How do I go about specifying and using an ENUM in a Django model?

Answer

fulmicoton picture fulmicoton · Aug 22, 2008

From the Django documentation:

MAYBECHOICE = (
    ('y', 'Yes'),
    ('n', 'No'),
    ('u', 'Unknown'),
)

And you define a charfield in your model :

married = models.CharField(max_length=1, choices=MAYBECHOICE)

You can do the same with integer fields if you don't like to have letters in your db.

In that case, rewrite your choices:

MAYBECHOICE = (
    (0, 'Yes'),
    (1, 'No'),
    (2, 'Unknown'),
)