CharField with a limited set of strings leads to inefficient data
storage since the string value is stored over and over on disk. MySQL’s
ENUM type allows a more compact representation of such columns by storing
the list of strings just once and using an integer in each row to refer to
which string is there.
EnumField allows you to use the
ENUM type with
A subclass of Django’s
Charfieldthat uses a MySQL
choicesis a standard Django argument for any field class, however it is required for
EnumField. It can either be a list of strings, or a list of two-tuples of strings, where the first element in each tuple is the value used, and the second the human readable name used in forms. For example:
from django_mysql.models import EnumField class BookCover(Model): color = EnumField(choices=['red', 'green', 'blue']) class Book(Model): color = EnumField(choices=[ ('red', 'Bright Red'), ('green', 'Vibrant Green'), 'blue', # human readable name will be set to "blue" ])
It is possible to append new values to
choicesin migrations, as well as edit the human readable names of existing choices.
However, editing or removing existing choice values will error if MySQL Strict Mode is on, and replace the values with the empty string if it is not.
Also the empty string has strange behaviour with
ENUM, acting somewhat like
NULL, but not entirely; therefore it’s recommended you have Strict Mode on.