我正在尝试在数据结构上调用Flask的jsonify
,但我得到TypeError: unorderable types: str() < builtin_function_or_method()
。如何解决此错误?
bucketlists = [{
'id': 1,
'name': "BucketList1",
'items': [{
id: 1,
'name': "I need to do X",
'date_created': "2015-08-12 11:57:23",
'date_modified': "2015-08-12 11:57:23",
'done': False
}],
'date_created': "2015-08-12 11:57:23",
'date_modified': "2015-08-12 11:57:23",
'created_by': "1113456"
}]
@app.route('/bucketlists/', methods=['GET'])
def get_bucketlists():
return jsonify({'bucketlists': bucketlists})
答案 0 :(得分:4)
id
是一个内置的Python函数; jsonify
无法序列化它,您需要用引号包装字典键以使其成为字符串:
bucketlists = [{
'id': 1,
'name': "BucketList1",
'items': [{
'id': 1, # -----> Here
'name': "I need to do X",
'date_created': "2015-08-12 11:57:23",
'date_modified': "2015-08-12 11:57:23",
'done': False
}],
'date_created': "2015-08-12 11:57:23",
'date_modified': "2015-08-12 11:57:23",
'created_by': "1113456"
}]
此外,您需要添加双下划线来访问模块的名称:
app = Flask(__name__)