How do I go about specifying and using an ENUM in a Django model?
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'),
)
A the top of your models.py file, add this line after you do your imports:
enum = lambda *l: [(s,_(s)) for s in l]
There're currently two github projects based on adding these, though I've not looked into exactly how they're implemented:
I don't think either use DB enum types, but they are in the works for first one.