django-enumfield 1.2.1

Custom Django field for using enumerations of named constants

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 = enum.EnumField(BeerStyle, default=BeerStyle.LAGER)
Beer.objects.create(style=BeerStyle.STOUT)
Beer.objects.filter(style=BeerStyle.STOUT)

You can use your own labels for Enum items

class Animals(enum.Enum):
    CAT = 1
    DOG = 2

    labels = {
        CAT: 'Cat',
        DOG: 'Dog'
    }

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 = enum.EnumField(PersonStatus)

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

person = Person.objects.create(status=PersonStatus.ALIVE)
try:
    person.status = PersonStatus.REANIMATED
    person.save()
except InvalidStatusOperationError:
    print "Person status can not go from ALIVE to REANIMATED"

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

    labels = {
        MALE: 'Male',
        FEMALE: 'Female',
    }

class PersonForm(forms.Form)
    gender = forms.TypedChoiceField(choices=GenderEnum.choices(), coerce=int)

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

MIT

Author

Hannes Ljungberg

Pip

django-enumfield==1.2.1

Classifiers

  • Environment :: Web Environment
  • Framework :: Django
  • Intended Audience :: Developers
  • License :: OSI Approved :: MIT License
  • Operating System :: OS Independent
  • Programming Language :: Python
  • Programming Language :: Python :: 2.7
  • Programming Language :: Python :: 3.3
  • Programming Language :: Python :: 3.4
  • Natural Language :: English
  • Topic :: Utilities
  • Topic :: Software Development :: Libraries :: Python Modules
File Type Python Version Uploaded On Downloads
django_enumfield-1.2.1-py2.py3-none-any.whl Wheel 2.7 May 10, 2015 4,169
django-enumfield-1.2.1.tar.gz Source May 10, 2015 2,672
Version Release Date
1.2.1 May 10, 2015
1.2 Feb. 20, 2015
1.1.2 Dec. 16, 2014
1.1.1 July 22, 2014
1.1 June 11, 2014
1.0.1 Jan. 29, 2014
1.0 Nov. 14, 2013
1.0c1 Jan. 31, 2013
Date Package Version Action
Aug. 24, 2015, 11:02 p.m. django-enumfield 1.1 Release Created
Aug. 24, 2015, 11:02 p.m. django-enumfield 1.2.1 Release Created
Aug. 24, 2015, 11:02 p.m. django-enumfield 1.0c1 Release Created
Aug. 24, 2015, 11:02 p.m. django-enumfield 1.2 Release Created
Aug. 24, 2015, 11:02 p.m. django-enumfield 1.0.1 Release Created
Aug. 24, 2015, 11:02 p.m. django-enumfield 1.0 Release Created
Aug. 24, 2015, 11:02 p.m. django-enumfield 1.1.2 Release Created
Aug. 24, 2015, 11:02 p.m. django-enumfield 1.1.1 Release Created
Aug. 24, 2015, 11:02 p.m. django-enumfield Package Created