我想弄清楚为什么日期在 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 -
这是用于编码的对象
course_record_model = ns.model('Model', {
'topic': fields.String,
'totalMinutes': fields.Integer,
'percentComplete': fields.Integer,
'lastUpdate': fields.DateTime,
})
注意 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()
