2014-03-25 38 views
4

我們使用Flask-Restful來實現一個API。作爲數據庫,我們使用MongoDB和MongoEngine作爲ODM。爲了讓MongoEngine與Restful協同工作,我們遵循this blog article。爲了獲得正確的json格式,我們使用了內建的marsheling-methods。這對於單個對象(例如集合中的一個項目)是完美的,但是當對對象列表(例如集合中的所有項目)進行封裝時,會引發(雖然我們使用與單個對象相同的語法)。這就是我們的模型和我們的觀點的樣子(我不粘貼路線,因爲他們在一個單獨的文件和工作)。來自集合的對象列表的屬性錯誤?

型號:

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

什麼是完整的追蹤?如果不知道'AttributeError'發生在哪裏以及哪個對象沒有哪個屬性,很難提供幫助。 – dirn

+0

見編輯!謝謝 – Tronic

回答

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 

我相信這是不可能用瓶的RESTful的編組支持返回一個普通列表,它總是希望一本字典。出於這個原因,我把這個列表放在「任務」鍵下。

我希望這會有所幫助。

+0

如果要編寫一個測試,發送多個任務的列表,該怎麼辦?似乎這種方法只會編組列表中的第一個對象。 @Miguel - 任何想法? –

+0

不知道我明白。該解決方案將整理整個列表。列表中的每個項目都將作爲嵌套實體進行處理。 – Miguel

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] 
相關問題