Django Model Choices using Python Enum

Python 3.4 introduced enums types which seem tailor made for use with django model choices. For some reason there seemed no examples to do this, but it’s relatively simple with some tricks (you may call them warts, django needs to enable this better). These have been back ported to prior python versions via the enum34 package (“pip install enum34”).

In this example, we’ll use an IntEnum to associate a variable with an integer value. For django, this means that the database contains integers, though we have a descriptive text for each choice. This is desirable, as we can abstract the name of the variable from the actual value. If you change / refactor the name, the existing values will simply remain the same.

Here’s the model definition:

from enum import IntEnum, unique

def django_enum(cls):
    # decorator needed to enable enums in django templates
    cls.do_not_call_in_templates = True
    return cls

class MyModel(Model):
    # store the choices in the model, so that they are readily 
    # accessible wherever the model is. 
    # unique ensures all variables are unique
    class MyChoices(IntEnum):
        ready = 0
        set = 10
        go = 20
        complete = 100

    # transform enum into choices conform format [(value, str)]
    # also replacing underscore with a space for readability
    status = IntegerField(choices=[(choice.value,"_", " ")) for choice in MyChoices])

You can now access this in your template as you would expect:

{% if mymodel.status == mymodel.MyChoices.ready %}
    We are ready! {{ }}
{% elif mymodel.status >= mymodel.MyChoices.set %}
    Set, go or complete: {{ mymodel.get_status_display }}
{% endif %}

In python, the enums can be accessed by their value or their name:

# let's display the name
# let's display the value

# python will automatically compare the int value when comparing. This returns True
MyModel.MyChoices.ready == 0

# this will return 'MyChoices.ready'

That’s all I’ve needed so far. Read up more on the official docs at and let me know if this can be improved in any way!

If you want to know why you need the django_enum decorator, read this:


Now read this

Apple removing outdated iPads and iPhones

After Apple’s presentation on March 21st 2016 it seems as if they have listened to my previous post on their line-up, at least with regards to the internals. All iPhones and all apart from one iPad now have the latest or previous... Continue →