gpt4 book ai didi

flask - 集合中对象列表的属性错误?

转载 作者:行者123 更新时间:2023-12-03 15:51:02 25 4
gpt4 key购买 nike

我们使用 Flask-Restful 来实现 API。作为数据库,我们使用 MongoDB 和 MongoEngine 作为 ODM。为了让 MongoEngine 与 Restful 一起工作,我们遵循了 this blog article .为了获得正确的 json 格式,我们使用内置 marsheling-methods .这非常适用于单个对象(例如集合中的一个项目),但是当编码对象列表(例如集合中的所有项目)时,会引发 AttributeError (尽管我们使用与对于单个对象)。这就是我们的模型和 View 的样子(我没有粘贴路由,因为它们在单独的文件中并且可以工作)。

型号:

class Task(db.Document):
name = db.StringField()
description_mini = db.StringField()

观看次数:

parser = reqparse.RequestParser()
parser.add_argument('task_id', type=str)

task_format = {
"name": fields.String,
"description_mini": fields.String
}

class TasksView(Resource):

@marshal_with(task_format)
def get(self):
tasks = Task.objects().all()
return tasks, 200


class TaskDetailView(Resource):

@marshal_with(task_format)
def get(self):
args = parser.parse_args()
startup_id = args['task_id']

task = Task.objects(id=task_id).first()

return task, 200

完整的堆栈跟踪:

AttributeError

Traceback (most recent call last)
File "/project/venv/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/project/venv/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/project/venv/lib/python2.7/site-packages/flask_restful/__init__.py", line 257, in error_router
return self.handle_error(e)
File "/project/venv/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/project/venv/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/project/venv/lib/python2.7/site-packages/flask_restful/__init__.py", line 257, in error_router
return self.handle_error(e)
File "/project/venv/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/project/venv/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/project/venv/lib/python2.7/site-packages/flask_restful/__init__.py", line 397, in wrapper
resp = resource(*args, **kwargs)
File "/project/venv/lib/python2.7/site-packages/flask/views.py", line 84, in view
return self.dispatch_request(*args, **kwargs)
File "/project/venv/lib/python2.7/site-packages/flask_restful/__init__.py", line 487, in dispatch_request
resp = meth(*args, **kwargs)
File "/project/venv/lib/python2.7/site-packages/flask_restful/__init__.py", line 562, in wrapper
return marshal(data, self.fields), code, headers
File "/project/venv/lib/python2.7/site-packages/flask_restful/__init__.py", line 533, in marshal
return OrderedDict(items)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/collections.py", line 52, in __init__
self.__update(*args, **kwds)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_abcoll.py", line 547, in update
for key, value in other:
File "/project/venv/lib/python2.7/site-packages/flask_restful/__init__.py", line 532, in <genexpr>
for k, v in fields.items())
File "/project/venv/lib/python2.7/site-packages/flask_restful/fields.py", line 104, in output
value = get_value(key if self.attribute is None else self.attribute, obj)
File "/project/venv/lib/python2.7/site-packages/flask_restful/fields.py", line 37, in get_value
return _get_value_for_keys(key.split('.'), obj, default)
File "/project/venv/lib/python2.7/site-packages/flask_restful/fields.py", line 42, in _get_value_for_keys
return _get_value_for_key(keys[0], obj, default)
File "/project/venv/lib/python2.7/site-packages/flask_restful/fields.py", line 51, in _get_value_for_key
return obj[key]
File "/project/venv/lib/python2.7/site-packages/mongoengine/queryset/base.py", line 152, in __getitem__
raise AttributeError
AttributeError

最佳答案

当您想要编码列表时,您还必须将字段定义为列表。

我认为这会起作用:

task_list_format = {
'tasks': fields.List(fields.Nested(task_format))
}

class TasksView(Resource):

@marshal_with(task_list_format)
def get(self):
tasks = Task.objects().all()
return { 'tasks': tasks }, 200

我相信使用 Flask-RESTful 中的编码支持不可能返回一个普通列表,它总是需要一个字典。出于这个原因,我将列表放在“任务”键下。

希望对您有所帮助。

关于flask - 集合中对象列表的属性错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22645029/

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