[英]Python Flask RestPlus Enum Type
Am using python 3.4.
我正在使用python 3.4.
I have created an enum
type as shown below:我创建了一个enum
类型,如下所示:
import enum
class EnumGender(enum.Enum):
blank = ' '
female = 'Female'
male = 'Male'
other = 'Other'
my question is how do i assign it to flask-restplus field as the only examples i can see are:我的问题是如何将它分配给 flask-restplus 字段,因为我能看到的唯一示例是:
fields.String(description='The object type', enum=['A', 'B'])
您可以为其分配成员名称:
fields.String(description='The object type', enum=EnumGender._member_names_)
I have opted for this approach:我选择了这种方法:
fields.String(attribute=lambda x: str(EnumGender(x.FieldContainingEnum).name))
(Source: How to get back name of the enum element in python? ) (来源: 如何在python中取回枚举元素的名称? )
Another possibility, Python 3.7.3 (circa Nov 2019):另一种可能性,Python 3.7.3(大约 2019 年 11 月):
fields.String(description="The object type",
enum=[x.name for x in EnumGender])
in my case having enum like this:在我的情况下有这样的枚举:
from enum import Enum
class SubscriptionEvent(Enum):
on_success = "ON_SUCCESS"
on_error = "ON_ERROR"
I needed definition in marshal like this:我需要像这样在元帅中定义:
subscription_action_serializer = api.model('SubscriptionAction', {
'event': fields.String(enum=[x.value for x in SubscriptionEvent], attribute='event.value'),
})
I needed just accept and present as valid enum values "ON_SUCCESS", "ON_SUCCESS"
我只需要接受并呈现为有效的枚举值"ON_SUCCESS", "ON_SUCCESS"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.