即使我的模型中描述了特定的序列化功能,我也遇到了java.lang.UnsatisfiedLinkError: org.cef.CefApp.N_PreInitialize()Z
at org.cef.CefApp.N_PreInitialize(Native Method)
at org.cef.CefApp.access$000(CefApp.java:24)
at org.cef.CefApp$1.run(CefApp.java:162)
at org.cef.CefApp.<init>(CefApp.java:167)
at org.cef.CefApp.getInstance(CefApp.java:220)
at org.cef.CefApp.getInstance(CefApp.java:207)
...
错误。
这是我的代码:
烧瓶路线(由React渲染):
menus.py
TypeError: Object of type 'datetime' is not JSON serializable
methods.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
最后是具有def Create_Menu(user):
menu = Menu(user=user)
db.session.add(menu)
db.session.commit()
return {"status": True,
"menu": 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]}
当我在后端使用Flask重新分配模板时,它可以工作,但是现在有了前端录入功能,它会因上面的错误而中断。
现在怎么了?为什么我的序列化功能不再起作用?
答案 0 :(得分:0)
请参见下文(使用自定义JSON编码器)
也请参见http://flask.pocoo.org/snippets/119/
import datetime
import json
class DateTimeEncoder(json.JSONEncoder):
def default(self, z):
if isinstance(z, datetime.datetime):
return (str(z))
else:
return super().default(z)
my_dict = {'date': datetime.datetime.now()}
print(json.dumps(my_dict,cls=DateTimeEncoder))
输出
{"date": "2019-06-12 15:44:14.978766"}