django-choices-enums用于 django 的枚举

联合创作 · 2023-09-28 10:50

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
浏览 21
点赞
评论
收藏
分享

手机扫一扫分享

编辑 分享
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

编辑 分享
举报