gpt4 book ai didi

Python NeoModel 如何将查询结果作为 JSON 对象返回

转载 作者:太空宇宙 更新时间:2023-11-04 04:52:10 25 4
gpt4 key购买 nike

我是 Python 的新手,在我的应用程序中,我使用 Neomodel 从 Neo4j 数据库加载和检索图形数据。

在我的一条 route ,我有以下功能:

@api_mod.route('/users')                                                            
def get_users():
users = User.nodes #Returns a NodeSet
list_of_users = list(users) #Converts the NodeSet object to a list
return json.dumps(dict(users = [user for user in list_of_users]))

我的用户类定义如下:

class User(StructuredNode):
user_id = StringProperty(unique_index=True, required=True)
logged_in = RelationshipTo('Environment', 'LOGGED_IN', model=LoginAction)
launched = RelationshipTo('Application', 'LAUNCHED', model=LaunchedAction)
entered = RelationshipTo('Application', 'ENTERED', model=EnteredAction)
accessed = RelationshipTo('Application', 'ACCESSED', model=AccessedAction)
exited = RelationshipTo('Application', 'EXITED', model=ExitedAction)
logged_out = RelationshipTo('Environment', 'LOGGED_OUT', model=LogoutAction)
timed_out = RelationshipTo('Environment', 'TIME_OUT', model=TimeoutAction)

def toJSON(self):
return dict(user_id = self.user_id)

我的期望是我的/users 路由将返回一个包含数据库中所有用户的 JSON 对象。我知道 NodeSet 对象不能序列化为 JSON 对象,因此我尝试将其转换为列表。但是现在当我运行时出现以下错误:“TypeError:‘User’类型的对象不是 JSON 可序列化的”

这是整个堆栈跟踪:追溯(最近一次通话): 文件“/Users/mdebarros/.virtualenvs/useractivitylogs/lib/python3.6/site-packages/flask/app.py”,第 1997 行,在 call 返回 self.wsgi_app(environ, start_response) 文件“/Users/mdebarros/.virtualenvs/useractivitylogs/lib/python3.6/site-packages/flask/app.py”,第 1985 行,在 wsgi_app 响应 = self.handle_exception(e) 文件“/Users/mdebarros/.virtualenvs/useractivitylogs/lib/python3.6/site-packages/flask/app.py”,第 1540 行,handle_exception 重新加注(exc_type,exc_value,tb) 文件“/Users/mdebarros/.virtualenvs/useractivitylogs/lib/python3.6/site-packages/flask/_compat.py”,第 33 行,重新提出 提升值(value) 文件“/Users/mdebarros/.virtualenvs/useractivitylogs/lib/python3.6/site-packages/flask/app.py”,第 1982 行,在 wsgi_app 响应 = self.full_dispatch_request() 文件“/Users/mdebarros/.virtualenvs/useractivitylogs/lib/python3.6/site-packages/flask/app.py”,第 1614 行,在 full_dispatch_request rv = self.handle_user_exception(e) 文件“/Users/mdebarros/.virtualenvs/useractivitylogs/lib/python3.6/site-packages/flask/app.py”,第 1517 行,handle_user_exception 重新加注(exc_type,exc_value,tb) 文件“/Users/mdebarros/.virtualenvs/useractivitylogs/lib/python3.6/site-packages/flask/_compat.py”,第 33 行,重新提出 提升值(value) 文件“/Users/mdebarros/.virtualenvs/useractivitylogs/lib/python3.6/site-packages/flask/app.py”,第 1612 行,在 full_dispatch_request rv = self.dispatch_request() 文件“/Users/mdebarros/.virtualenvs/useractivitylogs/lib/python3.6/site-packages/flask/app.py”,第 1598 行,在 dispatch_request 返回 self.view_functionsrule.endpoint 文件“/Users/mdebarros/PycharmProjects/useractivitylogs/app/api/routes.py”,第 16 行,在 get_users 返回 json.dumps(dict(users = [user for user in list_of_users])) 文件“/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/init.py”,第 231 行,在转储中 返回 _default_encoder.encode(obj) 文件“/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/encoder.py”,第 199 行,编码 block = self.iterencode(o, _one_shot=True) 文件“/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/encoder.py”,第 257 行,在 iterencode 返回 _iterencode(o, 0) 默认情况下,文件“/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/encoder.py”,第 180 行 o.类(class)姓名)类型错误:“用户”类型的对象不是 JSON 可序列化的

关于如何处理此问题并返回正确的 JSON 对象的任何帮助或建议?

提前感谢您的帮助。

--医学博士

最佳答案

如你所知,必须是用户类模型有一个方法来执行数据结构的翻译过程(序列化),要解决,一个解决方案是添加一个方法来执行此操作:

class User(StructuredNode):
user_id = StringProperty(unique_index=True, required=True)
logged_in = RelationshipTo('Environment', 'LOGGED_IN', model=LoginAction)
launched = RelationshipTo('Application', 'LAUNCHED', model=LaunchedAction)
entered = RelationshipTo('Application', 'ENTERED', model=EnteredAction)
accessed = RelationshipTo('Application', 'ACCESSED', model=AccessedAction)
exited = RelationshipTo('Application', 'EXITED', model=ExitedAction)
logged_out = RelationshipTo('Environment', 'LOGGED_OUT', model=LogoutAction)
timed_out = RelationshipTo('Environment', 'TIME_OUT', model=TimeoutAction)

@property
def serialize(self):
return {
'user_id': self.user_id,
'logged_in': self.logged_in,
'launched': self.launched,
'entered': self.entered,
'accessed': self.accessed,
'exited': self.exited,
'logged_out': self.logged_out,
'timed_out' : self.timed_out
}

现在,由于添加了方法,属性可以作为属性调用:

@api_mod.route('/users')                                                            
def get_users():
users = User.nodes
list_of_users = list(users)
return json.dumps(dict(json_users = [user.serialize for user in list_of_users]))

关于Python NeoModel 如何将查询结果作为 JSON 对象返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47985578/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com