我得到了这个TypeError: Object of type 'datetime' is not JSON serializable
错误,即使我在模型中描述了一个特定的序列化函数。
这是我的代码:
Flask路由(由React渲染):
menus.py
@menus_bp.route('/menus', methods=['GET', 'POST'])
def menus():
response_object = {
'status': 'fail',
'message': 'Invalid payload.'
}
try:
user = User.query.filter_by(id=1).first()
if user.menu == []:
return edit_menu()
else:
template = render_template('menus.html')
response_object = {
'status': 'success',
'message': 'success',
'data': [{"restaurant": user.restaurant,
"menu": menu,
"content": template}] # template passed to React
}
# db method
Create_Menu(user=user)
return jsonify(response_object), 200
except (exc.IntegrityError, ValueError):
db.session.rollback()
return jsonify(response_object), 400
方法.py
def Create_Menu(user):
menu = Menu(user=user)
db.session.add(menu)
db.session.commit()
return {"status": True,
"menu": menu}
最后是菜单模型,它有一个serialize()
函数:
class Menu(db.Model):
__tablename__='menu'
"""
Model for storing menus.
"""
id = db.Column(db.Integer, primary_key=True)
created = db.Column(db.DateTime, default=func.now(), nullable=False)
user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
def __init__(self, user):
self.user = user
def serialize(self):
return { 'id' : self.id,
'created': self.created,
'coffees' : [ item.serialize() for item in self.coffees]}
但我得到了如下的回溯:
TypeError: Object of type 'datetime' is not JSON serializable
File "/usr/lib/python3.6/site-packages/flask/app.py", line 2309, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/lib/python3.6/site-packages/flask/app.py", line 2295, in wsgi_app
response = self.handle_exception(e)
File "/usr/lib/python3.6/site-packages/flask_restful/__init__.py", line 269, in error_router
return original_handler(e)
File "/usr/lib/python3.6/site-packages/flask_cors/extension.py", line 161, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "/usr/lib/python3.6/site-packages/flask/app.py", line 1741, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/lib/python3.6/site-packages/flask/_compat.py", line 34, in reraise
raise value.with_traceback(tb)
File "/usr/lib/python3.6/site-packages/flask/app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "/usr/lib/python3.6/site-packages/flask/app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/lib/python3.6/site-packages/flask_restful/__init__.py", line 269, in error_router
return original_handler(e)
File "/usr/lib/python3.6/site-packages/flask_cors/extension.py", line 161, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "/usr/lib/python3.6/site-packages/flask/app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/lib/python3.6/site-packages/flask/_compat.py", line 34, in reraise
raise value.with_traceback(tb)
File "/usr/lib/python3.6/site-packages/flask/app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/lib/python3.6/site-packages/flask_debugtoolbar/__init__.py", line 125, in dispatch_request
return view_func(**req.view_args)
File "/usr/lib/python3.6/site-packages/flask_restful/__init__.py", line 462, in wrapper
return self.make_response(data, code, headers=headers)
File "/usr/lib/python3.6/site-packages/flask_restful/__init__.py", line 491, in make_response
resp = self.representations[mediatype](data, *args, **kwargs)
File "/usr/lib/python3.6/site-packages/flask_restful/representations/json.py", line 21, in output_json
dumped = dumps(data, **settings) + "\n"
File "/usr/lib/python3.6/json/__init__.py", line 238, in dumps
**kw).encode(obj)
File "/usr/lib/python3.6/json/encoder.py", line 201, in encode
chunks = list(chunks)
File "/usr/lib/python3.6/json/encoder.py", line 430, in _iterencode
yield from _iterencode_dict(o, _current_indent_level)
File "/usr/lib/python3.6/json/encoder.py", line 404, in _iterencode_dict
yield from chunks
File "/usr/lib/python3.6/json/encoder.py", line 404, in _iterencode_dict
yield from chunks
File "/usr/lib/python3.6/json/encoder.py", line 325, in _iterencode_list
yield from chunks
File "/usr/lib/python3.6/json/encoder.py", line 404, in _iterencode_dict
yield from chunks
File "/usr/lib/python3.6/json/encoder.py", line 325, in _iterencode_list
yield from chunks
File "/usr/lib/python3.6/json/encoder.py", line 404, in _iterencode_dict
yield from chunks
File "/usr/lib/python3.6/json/encoder.py", line 437, in _iterencode
o = _default(o)
File "/usr/lib/python3.6/json/encoder.py", line 180, in default
o.__class__.__name__)
TypeError: Object of type 'datetime' is not JSON serializable
当我在后端使用Flask时,它曾经工作过,但是现在使用前端请求,它打破了上面的错误。
为什么我的serialize函数不再工作了?
3条答案
按热度按时间xpszyzbs1#
如已接受答案中的注解所指出,您可以简单地用途:
除非你想让事情变得更复杂。
2uluyalo2#
参见下文(使用自定义JSON编码器)
也参见http://flask.pocoo.org/snippets/119/
输出
yptwkmov3#
使用str(日期时间字段),它可以工作