Custom Django field for using enumerations of named constants
Project description
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django-enumfield-1.2.1.tar.gz.
File metadata
- Download URL: django-enumfield-1.2.1.tar.gz
- Upload date:
- Size: 9.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
66bdba5258e84f0ef7f59d5508e228388530c0ed1107a856fc93a963a3e9e6cc
|
|
| MD5 |
f928bee9f399df58bef80e7c7abe1c51
|
|
| BLAKE2b-256 |
0ed75f2e4b4dc736c789e452ac8a3a7419a548cd2ce96cebda5d10c467d4b3fc
|
File details
Details for the file django_enumfield-1.2.1-py2.py3-none-any.whl.
File metadata
- Download URL: django_enumfield-1.2.1-py2.py3-none-any.whl
- Upload date:
- Size: 28.0 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
65562dca1d85d41cb638e07009f42add004a2c79642dd663431f36236c10bd09
|
|
| MD5 |
eb28e0b99ff2fca1eb3e3b10906b91d0
|
|
| BLAKE2b-256 |
957c5859dae407160779b049a91ac1174b5cd11c4de1bd8381a91c88ffd5c608
|