Skip to main content

Custom Django field for using enumerations of named constants

Project description

django-enumfield
==============

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

.. image:: https://travis-ci.org/5monkeys/django-enumfield.png?branch=master
:target: http://travis-ci.org/5monkeys/django-enumfield

Installation
------------

Install django-enumfield in your python environment

.. code:: sh

$ pip install django-enumfield

Usage
-----

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

.. code:: python

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)

.. code:: python

Beer.objects.create(style=BeerStyle.STOUT)
Beer.objects.filter(style=BeerStyle.STOUT)

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

.. code:: python

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.

.. code:: python

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.

.. code:: python

from django.forms import Form
from django_enumfield import enum

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

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.

Project details


Release history Release notifications

History Node

1.3b2

History Node

1.3b1

History Node

1.2.1

History Node

1.2

History Node

1.1.2

History Node

1.1.1

History Node

1.1

History Node

1.0.1

This version
History Node

1.0

History Node

1.0c1

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
django-enumfield-1.0.tar.gz (8.7 kB) Copy SHA256 hash SHA256 Source None Nov 14, 2013

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging CloudAMQP CloudAMQP RabbitMQ AWS AWS Cloud computing Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page