django-choices-enums -- 用于 django choices 的枚举类型

分类: 个人程序 , python

2019-11-09

|

154

|

评论:0

分享:

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



本文共 0 个回复

发表评论 (对文章评论)

captcha