我试图找出为什么日期在 Flask-restplus 中不起作用。
MarshallingError: Unable to marshal field "lastUpdate" value "<built-in method now of type object at 0x10264d6e0>": Unsupported DateTime format
127.0.0.1 - - [16/Apr/2016 22:24:18] "POST /api/v1/course/record/ab HTTP/1.1" 500 -
这是用于编码(marshal)的对象
course_record_model = ns.model('Model', {
'topic': fields.String,
'totalMinutes': fields.Integer,
'percentComplete': fields.Integer,
'lastUpdate': fields.DateTime,
})
注意字段。日期时间。这就是有问题的地方。
def __init__(self, courseid, memberid, **kwargs):
"""Create instance."""
db.Model.__init__(self, **kwargs)
self.courseID = courseid
self.memberID = memberid
self.lastUpdate = datetime.datetime.now
我尝试添加一些格式,但似乎没有帮助,这里是文档
class fields.DateTime(dt_format='rfc822', **kwargs) Return a formatted datetime string in UTC. Supported formats are RFC 822 and ISO 8601.
See email.utils.formatdate() for more info on the RFC 822 format.
See datetime.datetime.isoformat() for more info on the ISO 8601 format.
Parameters: dt_format (str) – 'rfc822' or 'iso8601'
不确定从 API 调用传入时如何设置日期格式。
最佳答案
如您所见,您有 "<built-in method now of type object at 0x10264d6e0>"
而不是datetime
对象。
我怀疑您的代码中的某个地方忘记输入括号 ()
像这样:
someobject.lastUpdate = datetime.now
但应该是
someobject.lastUpdate = datetime.now()
关于python - 修复从休息端点编码(marshal)数据时的 "unsupported date format",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36672157/