【已解决】Flask-restful中想要使用marshal格式化enum变量

对于:

task_fields = {
    ‘id’: fields.String,
    ‘promotionCode’ : fields.String,
    ‘hasFinished’ : fields.Boolean,
    ‘initiatorId’ : fields.String,
    ‘errandorId’ : fields.String,
}

想要添加输出对应的枚举类型变量:

class TaskStatus(enum.Enum):
    Created = "Created"
    Publishing = "Publishing"
    Started = "Started" # == task grabbed -> consider started
    ToBeDetermine = "ToBeDetermine" # after upload info, need initiator determine
    Delivering = "Delivering" # after upload info, no need initiator determine
    Completed = "Completed"
    Canceled = "Canceled"
statusType = db.Column(db.Enum(TaskStatus), default=TaskStatus.Created)

flask-restful marshal enum

How do I marshal a list of objects? · Issue #300 · flask-restful/flask-restful · GitHub

python – Flask-restful, marshal_with + nested data – Stack Overflow

Output Fields — Flask-RESTful 0.2.1 documentation

-》

或许可以自己去实现对应的自定义字段,

然后把enum转换为对应的字符串?

API Docs — Flask-RESTful 0.2.1 documentation

去看看:

Fields

有哪些值,是否有enum

  • fields.String
  • fields.FormattedString
  • fields.Url
  • fields.DateTime
  • fields.Float
  • fields.Integer
  • fields.Arbitrary
  • fields.Nested
  • fields.List
  • fields.Raw
  • fields.Boolean
  • fields.Fixed
  • fields.Price

此处,由于enum是字符串

所以去试试:

(1)直接使用fields.String,看看是否可以正常输出对应的字符串

‘statusType’ : fields.String,

貌似没有报错。

(2)尝试fields.Raw

‘itemType’ : fields.Raw,

报错了:

  File "/usr/local/lib/python2.7/json/encoder.py", line 184, in default
    raise TypeError(repr(o) + " is not JSON serializable")
TypeError: <ItemType.Small: ‘Small’> is not JSON serializable

改为fields.String,

不过后来输出的信息是:

    "statusType": "TaskStatus.Created",
    "itemType": "ItemType.Small",

不是我此处想要的:

只保留字段,不保留类型

(3)尝试去实现自定义的格式,把enum转换为字符串

结果通过自定义:

class EnumItem(fields.Raw):
    def format(self, value):
        gLog.debug("value=%s", value)
        return "%s" % (value)
promotion_fields = {
    ‘id’ : fields.String,
    ‘sourceType’ : EnumItem(attribute="sourceType"),
    ‘type’ : EnumItem(attribute="type"),
}

结果此处还是输出的是:

"itemType": "ItemType.Small",
"statusType": "TaskStatus.Created",
DEBUG in Task [/root/RunningFast/staging/runningfast/resources/Task.py:40]:
value=ItemType.Small

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in Task [/root/RunningFast/staging/runningfast/resources/Task.py:40]:
value=RatingType.OneStar

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in Task [/root/RunningFast/staging/runningfast/resources/Task.py:40]:
value=BillType.Initiator

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in Task [/root/RunningFast/staging/runningfast/resources/Task.py:40]:
value=TipType.NoTip

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in Task [/root/RunningFast/staging/runningfast/resources/Task.py:40]:
value=TaskStatus.Created

所以此处就变成了:

【已解决】Python中如何让Enum的字符串输出字段的值而不带类型的前缀

然后此处就可以得到所需要的:

value=Small

了。



发表评论

电子邮件地址不会被公开。 必填项已用*标注

无觅相关文章插件,快速提升流量