V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
a719114136
V2EX  ›  Django

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

  •  
  •   a719114136 ·
    gojuukaze · Nov 9, 2019 · 3588 views
    This topic created in 2368 days ago, the information mentioned may be changed or developed.

    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
    
    
    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2483 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 07:40 · PVG 15:40 · LAX 00:40 · JFK 03:40
    ♥ Do have faith in what you're doing.