make enum34 package play well with django choices
Project description
django-choices-enum
===================
django-choices-enum allows the usage of the 2.x backport of Python 3.4's enum
package (enum34) as choices in Django models.
from django.db import models
from django_choices_enum import ChoicesEnum
class Monster(models.Model):
class Types(str, ChoicesEnum):
BEAST = ('beast', 'Beast')
UNDEAD = ('undead', 'Undead')
HUMAN = ('human', 'Human')
name = models.CharField(max_length=40)
type = models.CharField(max_length=40, choices=Types.choices())
You may now write code like this:
monsters = Monster.objects.filter(type=Monster.Types.BEAST)
for m in monsters:
assert m.type == Monster.Types.BEAST
m = Monster(name='Vampire', type=Monster.Types.UNDEAD)
m.save()
Details
=======
Each enumeration is a class subclassed from the type of the field and the
ChoicesEnum type:
class MyIntEnumeration(int, ChoicesEnum):
A = (1, 'Label A')
Each constant is then assigned a tuple of (value, label) as expected by
the Django choices parameter. If the constant and label are the same,
you may omit the tuple and simply set the constant equal to the value of
the choice.
Contributions
=============
I did not really write most of this code. It was found and fixed from
a Gist:
https://gist.github.com/dstufft/5600529
===================
django-choices-enum allows the usage of the 2.x backport of Python 3.4's enum
package (enum34) as choices in Django models.
from django.db import models
from django_choices_enum import ChoicesEnum
class Monster(models.Model):
class Types(str, ChoicesEnum):
BEAST = ('beast', 'Beast')
UNDEAD = ('undead', 'Undead')
HUMAN = ('human', 'Human')
name = models.CharField(max_length=40)
type = models.CharField(max_length=40, choices=Types.choices())
You may now write code like this:
monsters = Monster.objects.filter(type=Monster.Types.BEAST)
for m in monsters:
assert m.type == Monster.Types.BEAST
m = Monster(name='Vampire', type=Monster.Types.UNDEAD)
m.save()
Details
=======
Each enumeration is a class subclassed from the type of the field and the
ChoicesEnum type:
class MyIntEnumeration(int, ChoicesEnum):
A = (1, 'Label A')
Each constant is then assigned a tuple of (value, label) as expected by
the Django choices parameter. If the constant and label are the same,
you may omit the tuple and simply set the constant equal to the value of
the choice.
Contributions
=============
I did not really write most of this code. It was found and fixed from
a Gist:
https://gist.github.com/dstufft/5600529
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
File details
Details for the file django-choices-enum-0.2.tar.gz
.
File metadata
- Download URL: django-choices-enum-0.2.tar.gz
- Upload date:
- Size: 3.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5ad88f4bdb03b2f7af4ab1352f37d4b63adb350965961de2d57c39d53ae0ab85 |
|
MD5 | 145383d1eceb52e44b0e892471c8d284 |
|
BLAKE2b-256 | 585c61f4a751fbc78424e97b93f69b8ccd92bb71077f15c23bf4709efba32833 |