项目作者: gojuukaze

项目描述 :
The enum type used for Django choices enables Django's choices to support code prompts (用于django choices的枚举类型,支持代码提示)
高级语言: Python
项目地址: git://github.com/gojuukaze/django-choices-enums.git
创建时间: 2019-11-04T07:36:34Z
项目社区:https://github.com/gojuukaze/django-choices-enums

开源协议:GNU General Public License v3.0

下载


django-choices-enums

The enum type used for Django choices enables Django’s choices to support code prompts!!

用于django choices的枚举类型,使得django的choices支持代码提示!!

feature

  • the human-readable name of choices not missing. 不缺失 choices 的可读说明
  • code hints. 能支持代码提示
  • Lightweight, easy to use. 方便使用、轻量、无侵入

依赖

  • python3+

Installation

  1. pip install django-choices-enums

Usage

  1. from django_choices_enums import DjangoChoicesEnum
  2. class TypeChoices(DjangoChoicesEnum):
  3. Created = (1,'created')
  4. Finished = (2,'finished')
  5. class Foo(models.Model):
  6. type = models.IntegerField(choices=TypeChoices.to_django_choices())
  • use enum like this:
  1. f = Foo.create(type=TypeChoices.Created)
  • get all values:
  1. print(TypeChoices.all_values())
  2. # Out: (1, 2)
  • anonymous values:
  1. from django_choices_enums import DjangoChoicesEnum
  2. class MyEnum(DjangoChoicesEnum):
  3. A = (1, 'xxx')
  4. B = (2, 'xxx')
  5. anonymous = ((3, 'xx'),
  6. (4, 'xx'),
  7. )
  8. print(MyEnum.to_django_choices())
  9. # Out: ((1, 'xxx'), (2, 'xxx'), (3, 'xx'), (4, 'xx'))
  • get verbose:
  1. from django_choices_enums import DjangoChoicesEnum
  2. class MyEnum(DjangoChoicesEnum):
  3. A = (1, 'A ...')
  4. B = (2, 'B ...')
  5. anonymous = ((3, '33'),
  6. (4, '44'),
  7. )
  8. print(MyEnum.A.verbose)
  9. # Out: A ...
  10. print(MyEnum.get_verbose(2))
  11. # Out: B ...
  12. print(MyEnum.get_verbose(3))
  13. # Out: 33
  14. print(MyEnum.get_verbose(MyEnum.B))
  15. # Out: B ...