Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Flask RestPlus Enum Type

Am using python 3.4. I have created an enum type as shown below:

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:

fields.String(description='The object type', enum=['A', 'B'])

like image 953
Ndurere David Avatar asked Jan 20 '18 12:01

Ndurere David


2 Answers

You can assign the member names to it:

fields.String(description='The object type', enum=EnumGender._member_names_)
like image 197
Kobi Dadon Avatar answered Nov 17 '22 22:11

Kobi Dadon


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?)

like image 4
peterrus Avatar answered Nov 17 '22 23:11

peterrus