django-choices-enums 是用于django的枚举。
此实现特点:
不缺失choices的可读说明能支持代码提示轻量,方便使用,无侵入安装pipinstalldjango-choices-enums使用完整文档见:https://github.com/gojuukaze/django-choices-enums
fromdjango_choices_enumsimportDjangoChoicesEnumclassTypeChoices(DjangoChoicesEnum):Created=(1,'created')Finished=(2,'finished')anonymous=((3,'xx'),(4,'xx'),)classFoo(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:createdprint(TypeChoices.get_verbose(2))#Out:finishedprint(TypeChoices.get_verbose(3))#Out:xxprint(TypeChoices.get_verbose(TypeChoices.B))#Out:finished
评论