当前位置:  开发笔记 > 编程语言 > 正文

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

如何解决《集合中对象列表的属性错误?》经验,为你挑选了1个好方法。

我们使用Flask-Restful来实现API.作为数据库,我们使用MongoDB和MongoEngine作为ODM.为了让MongoEngine与Restful一起工作,我们遵循了这篇博客文章.为了获得正确的json格式,我们使用内置marsheling方法.这适用于单个对象(例如集合中的一个项目),但是当对一个对象列表进行编组(例如集合的所有项目)时,会引发AttributeError(尽管我们使用与单个对象相同的语法).这就是我们的模型和我们的视图的样子(我不粘贴路由,因为它们在一个单独的文件中工作).

模型:

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 
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

Miguel.. 6

如果要编组列表,还必须将字段定义为列表.

我认为这会奏效:

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中的编组支持返回一个普通列表,它总是需要一个字典.出于这个原因,我把列表放在"任务"键下.

我希望这有帮助.



1> Miguel..:

如果要编组列表,还必须将字段定义为列表.

我认为这会奏效:

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中的编组支持返回一个普通列表,它总是需要一个字典.出于这个原因,我把列表放在"任务"键下.

我希望这有帮助.

推荐阅读
夏晶阳--艺术
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有