Specifying a mySQL ENUM in a Django model


Question

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

1
90
10/14/2013 2:31:23 PM

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'),
)
106
9/16/2016 1:43:42 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon