[Fixed]-Django – Handling "enum models"

10👍

Django ORM checks integrity if you specify choices attribute (when you insert/update data via user forms).

You also can set validation logic to database level and use database ENUM field if you db support this.

UPD:

class EnumField(models.Field):

    def __init__(self, *args, **kwargs):
        super(EnumField, self).__init__(*args, **kwargs)
        if not self.choices:
            raise AttributeError('EnumField requires `choices` attribute.')

    def db_type(self):
        return "enum(%s)" % ','.join("'%s'" % k for (k, _) in self.choices)

GENDER_MALE = 'm'
GENDER_FEMALE = 'f'
GENDER_CHOICES = (
    (GENDER_MALE, 'Male'),
    (GENDER_FEMALE, 'Female'),
)


class Person(models.Model):
    name = models.CharField(max_length=50)
    gender = EnumField(choices=GENDER_CHOICES)
👤San4ez

3👍

I have also a use case for database-backed Enum models. I came up with a solution that uses Python descriptors to implement django choices-like interface to the enum values:

from django.db import models

class ModelInstance:
    """Descriptor that uses predefined value fields to access model instances."""
    def __init__(self, **kwargs):
        self._instance_attributes = kwargs
        self._instance = None
    def __get__(self, instance, instance_type):
        if not self._instance:
            self._instance = instance_type.objects.get(**self._instance_attributes)
        return self._instance

class EnumModel(models.Model):
    name = models.CharField(max_length=100)
    JOHN = ModelInstance(name='John')
    FRED = ModelInstance(name='Fred')

JOHN and FRED are lazily evaluated model instances obtained for specific query params (name=value). I assume that the queries evaluate to unique rows in the table.

To use it, you first need to add rows to the database for John and Fred:

In [1]: EnumModel.objects.create(name="Fred")
In [2]: EnumModel.objects.create(name="John")

Then you can then access the model instances using the class attributes:

In [3]: EnumModel.JOHN                                                                                                                  
Out[3]: <EnumModel: EnumModel object (1)>
In [4]: EnumModel.FRED                                                                                                                  
Out[4]: <EnumModel: EnumModel object (2)>
👤btel

Leave a comment