django-choices-enums 是用于 django 的枚举。
此实现特点:
pip install django-choices-enums
完整文档见:https://github.com/gojuukaze/django-choices-enums
from django_choices_enums import DjangoChoicesEnum
class TypeChoices(DjangoChoicesEnum):
Created = (1,'created')
Finished = (2,'finished')
anonymous = ((3, 'xx'),
(4, 'xx'),
)
class Foo(models.Model):
type = models.IntegerField(choices = TypeChoices.to_django_choices() )
f = Foo.create(type=TypeChoices.Created)
print(TypeChoices.all_values())
# Out: (1, 2, 3, 4)
print(TypeChoices.Created.verbose)
# Out: created
print(TypeChoices.get_verbose(2))
# Out: finished
print(TypeChoices.get_verbose(3))
# Out: xx
print(TypeChoices.get_verbose(TypeChoices.B))
# Out: finished