Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
tag: 1.0c1
Fetching contributors…

Cannot retrieve contributors at this time

83 lines (55 sloc) 2.044 kb

django-enumfield

Provides an enumeration Django model field (using IntegerField) with reusable enums and transition validation.

Installation

Install django-enumfield in your python environment

$ pip install django-enumfield

Usage

Create an Enum-class and pass it as first argument to the Django model EnumField.

from django.db import models
from django_enumfield import enum

class BeerStyle(enum.Enum):
    LAGER = 0
    STOUT = 1
    WEISSBIER = 2

class Beer(models.Model):
    style = EnumField(BeerStyle, default=BeerStyle.LAGER)
Beer.objects.create(style=BeerStyle.STOUT)
Beer.objects.filter(style=BeerStyle.STOUT)

The Enum-class provides the possibility to use transition validation.

from django.db import models
from django_enumfield import enum

class PersonStatus(enum.Enum):
    ALIVE = 1
    DEAD = 2
    REANIMATED = 3

    _transitions = {
        DEAD: (ALIVE,),
        REANIMATED: (DEAD,)
    }

class Person(models.Model):
    status = EnumField(PersonStatus)

These transitions state that a PersonStatus can only go to DEAD from ALIVE and to REANIMATED from DEAD.

The Enum-class can also be used without the EnumField. This is very useful in Django form ChoiceFields.

from django.forms import Form
from django_enumfield import enum

class GenderEnum(enum.Enum):
    MALE = 1
    FEMALE = 2

class PersonForm(forms.Form)
    gender = forms.ChoiceField(choices=GenderEnum.choices())

Rendering PersonForm in a template will generate a select-box with "Male" and "Female" as option labels for the gender field.

Jump to Line
Something went wrong with that request. Please try again.