V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
a719114136
V2EX  ›  Django

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

  •  
  •   a719114136 ·
    gojuukaze · 2019-11-09 11:27:58 +08:00 · 2854 次点击
    这是一个创建于 1623 天前的主题,其中的信息可能已经有所发展或是发生改变。

    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
    
    
    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2854 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 14:18 · PVG 22:18 · LAX 07:18 · JFK 10:18
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.