django-choices-enums 介绍
django-choices-enums
django-choices-enums 是用于
django 的枚举。
此实现特点:
- 不缺失 choices 的可读说明
- 能支持代码提示
- 轻量,方便使用,无侵入
安装
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
django-choices-enums 官网
https://gitee.com/gojuukaze/django-choices-enums
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。