Provides an enumeration Django model field (using IntegerField) with reusable enums and transition validation.
Installation
Currently, we test on supported versions of django and Python versions 2.7, 3.5.
Install django-enumfield in your Python environment:
$ pip install django-enumfield
For testing with Django 1.9rc1, use
$ pip install --pre django-enumfield==1.3b2
If you are looking for native enum (or enum34) support, try testing Pull Request #26 which is planned for Django 1.10+.
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.
Metadata
Release files for django10-enumfield 1.4.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| django10-enumfield-1.4.1.tar.gz | 21.9 kB | Without provenance | Details |
Release files / django10-enumfield-1.4.1.tar.gz
| Download URL | django10-enumfield-1.4.1.tar.gz |
|---|---|
| Size | 21.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d45966bd585f3a4d209ce9c345ff6c0856173864841b1c1ece8faafa2c15d6b9
|
|
BLAKE2b-256 checksum How to use checksums |
9a14a86cf454d6913e6216ffc4a1879a7bf5aa3b82d7bbcb83a30093684ab054
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for django10-enumfield 1.4.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.