Real Python Enums for Django.
Project description
This package lets you use real Python (PEP435-style) enums with Django.
Installation
pip install django-enumfields
Included Tools
EnumField, EnumIntegerField
from enumfields import EnumField
from enumfields import Enum # Uses Ethan Furman's "enum34" backport
class Color(Enum):
RED = 'r'
GREEN = 'g'
BLUE = 'b'
class MyModel(models.Model):
color = EnumField(Color, max_length=1)
Elsewhere:
m = MyModel.objects.filter(color=Color.RED)
EnumIntegerField works identically, but the underlying storage mechanism is an IntegerField instead of a CharField.
Usage in Forms
Call the formfield method to use an EnumField directly in a Form.
class MyForm(forms.Form):
color = EnumField(Color, max_length=1).formfield()
Enum
Normally, you just use normal PEP435-style enums, however, django-enumfields also encludes its own version of Enum with a few extra bells and whistles. Namely, the smart definition of labels which are used, for example, in admin dropdowns. By default, it will create labels by title-casing your constant names. You can provide custom labels with a nested “Labels” class.
from enumfields import EnumField, Enum # Our own Enum class
class Color(Enum):
RED = 'r'
GREEN = 'g'
BLUE = 'b'
class Labels:
RED = 'A custom label'
class MyModel(models.Model):
color = EnumField(Color, max_length=1)
assert Color.GREEN.label == 'Green'
assert Color.RED.label == 'A custom label'
EnumFieldListFilter
enumfields.admin.EnumFieldListFilter is provided to allow using enums in list_filter.
from enumfields.admin import EnumFieldListFilter
class MyModelAdmin(admin.ModelAdmin):
list_filter = [('color', EnumFieldListFilter)]
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
File details
Details for the file django-enumfields-0.9.0.tar.gz
.
File metadata
- Download URL: django-enumfields-0.9.0.tar.gz
- Upload date:
- Size: 7.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ed6f388efc19610997f430406df4c717a70d91f64855ec337552488b1ced666c |
|
MD5 | 191f7ea64fbcfa3bb14058fbca771646 |
|
BLAKE2b-256 | 15b185adc04df5be5f594fe15015c0569429bdbd9aff9be3da70366ef054687f |
File details
Details for the file django_enumfields-0.9.0-py2.py3-none-any.whl
.
File metadata
- Download URL: django_enumfields-0.9.0-py2.py3-none-any.whl
- Upload date:
- Size: 9.8 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d517e796a298b63c55863c65d162298b8f07e8d0233cc1014db09b0d6aa0545f |
|
MD5 | 51bf75cb938a5130888315436790d60f |
|
BLAKE2b-256 | 96ee78cc4aeb3c4cb1fa98f895d6f2ce247d6ca2bae185d5c417dd502518e0ea |