Set model's TextChoices to a MySQL's ENUM

class TypesOfMedical(models.TextChoices):
    Hospital = 'Hospital', _('Hospital')
    Clinic = 'Clinic', _('Clinic')
    Pharmacy = 'Pharmacy', _('Pharmacy')

class Medical(models.Model):
    name = models.CharField(max_length=250)
    type = models.CharField(
        max_length=20,
        choices=TypesOfMedical.choices,
        default=TypesOfMedical.Hospital,
    )    

But this set the MySQL table column’s type as varchar(20) instead of ENUM('Hospital','Clinic','Pharmacy') - why is this so ?
Wouldn’t SQL search be faster if its an ENUM of n types instead of a text search ? Maybe not, but I feel a MySQL structure is more understandable than text.

You can check some reasons for this decision in Simon’s comment, there is also a separate ticket for adding EnumField:
https://code.djangoproject.com/ticket/24342

If you want to use the enum datatype you can use EnumField provided by a 3rd-party package django-mysql, see

https://django-mysql.readthedocs.io/en/latest/model_fields/enum_field.html

2 Likes