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

时间:2014-03-25 20:00:30

标签: flask mongoengine bson flask-restful

我们正在使用Flask-Restful来实现API。作为数据库,我们使用MongoDB和MongoEngine作为ODM。为了让MongoEngine与Restful一起工作,我们遵循了this blog article。为了获得正确的json格式,我们使用了内置marsheling-methods。这适用于单个对象(例如集合中的一个项目),但是当对一个对象列表进行编组(例如集合的所有项目)时,会引发 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 <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

3 个答案:

答案 0 :(得分: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中的编组支持返回一个普通列表,它总是需要一个字典。出于这个原因,我将列表放在&#34;任务&#34;键。

我希望这会有所帮助。

答案 1 :(得分:0)

尝试flask_restful.marshal_with_fields

>>> from flask_restful import marshal_with_field, fields
>>> @marshal_with_field(fields.List(fields.Integer))
... def get():
...     return ['1', 2, 3.0]
...
>>> get()
[1, 2, 3]

答案 2 :(得分:0)

据我了解,问题在于mongoengine的Queryset对象懒惰地查询数据库,并且Flask-restful / restplus编组期望一个列表。

我可以配合使用

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

class TasksView(Resource):

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