且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将WTForms与Enum一起使用

更新时间:2023-01-07 08:58:53

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.

在我看来,强制性选择将同时应用于(x,y)的两个(x,y)。

It seems to me that coerce will be applied to both (x,y) for (x,y) in choices.

我似乎无法理解为什么我一直看到:不能强迫None< type'NoneType'> 虽然

I can't seem to understand why I keep seeing: Can't coerce None <type 'NoneType'> though