I have the following code:
class Company(enum.Enum):
EnterMedia = 'EnterMedia'
WhalesMedia = 'WhalesMedia'
@classmethod
def choices(cls):
return [(choice.name, choice.name) for choice in cls]
@classmethod
def coerce(cls, item):
print "Coerce", item, type(item)
if item == 'WhalesMedia':
return Company.WhalesMedia
elif item == 'EnterMedia':
return Company.EnterMedia
else:
raise ValueError
And this is my wtform field:
company = SelectField("Company", choices=Company.choices(), coerce=Company.coerce)
This is the html generated in my form:
<select class="" id="company" name="company" with_label="">
<option value="EnterMedia">EnterMedia</option>
<option value="WhalesMedia">WhalesMedia</option>
</select>
Somehow, when I click submit, I keep getting "Not a Valid Choice".
This is my terminal output:
When I look at my terminal I see the following:
Coerce None <type 'NoneType'>
Coerce EnterMedia <type 'unicode'>
Coerce EnterMedia <type 'str'>
Coerce WhalesMedia <type 'str'>
class Company(enum.Enum):
WhalesMedia = 'WhalesMedia'
EnterMedia = 'EnterMedia'
@classmethod
def choices(cls):
return [(choice, choice.value) for choice in cls]
@classmethod
def coerce(cls, item):
"""item will be both type(enum) AND type(unicode).
"""
if item == 'Company.EnterMedia' or item == Company.EnterMedia:
return Company.EnterMedia
elif item == 'Company.WhalesMedia' or item == Company.WhalesMedia:
return Company.WhalesMedia
else:
print "Can't coerce", item, type(item)
So I hacked around and this works.
It seems to me that coerce will be applied to both (x,y) for (x,y) in choices.
I can't seem to understand why I keep seeing: Can't coerce None <type 'NoneType'>
though