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())
获取说明
print(TypeChoices.Created.verbose)
print(TypeChoices.get_verbose(2))
print(TypeChoices.get_verbose(3))
print(TypeChoices.get_verbose(TypeChoices.B))