Flask/Pymongo/Restplus - When using update(**data) I get "IndexError: list index out of range"

Flask/Pymongo/Restplus - When using update(**data) I get "IndexError: list index out of range"

烧瓶==1.1.1 pymongo==3.10.1 flask-restplus==0.13.0

我正在尝试将 JSON 放入 URL 并使用 update(),但我 运行 遇到了问题。在 routes.py 我有一个简单的 API 设置...

@api.route('/api/content/<idx>')
class UpdateContent(Resource):

    def put(self,idx):
        data = api.payload
        Content.objects(content_id=idx).update(**data)
        return jsonify(Content.objects(content_id=idx))

我正在使用邮递员 /api/content/2

  {
    "content_id": 2,
    "title": "Test 2",
    "description": "Test 2"
  }

我收到了这个...

Traceback (most recent call last):
  File "/Users/jyoseph/Sites/testsite/venv/lib/python3.8/site-packages/flask/app.py", line 2463, in __call__
    return self.wsgi_app(environ, start_response)
  File "/Users/jyoseph/Sites/testsite/venv/lib/python3.8/site-packages/flask/app.py", line 2449, in wsgi_app
    response = self.handle_exception(e)
  File "/Users/jyoseph/Sites/testsite/venv/lib/python3.8/site-packages/flask_restplus/api.py", line 584, in error_router
    return original_handler(e)
  File "/Users/jyoseph/Sites/testsite/venv/lib/python3.8/site-packages/flask/app.py", line 1866, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/jyoseph/Sites/testsite/venv/lib/python3.8/site-packages/flask/_compat.py", line 38, in reraise
    raise value.with_traceback(tb)
  File "/Users/jyoseph/Sites/testsite/venv/lib/python3.8/site-packages/flask/app.py", line 2446, in wsgi_app
    response = self.full_dispatch_request()
  File "/Users/jyoseph/Sites/testsite/venv/lib/python3.8/site-packages/flask/app.py", line 1951, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Users/jyoseph/Sites/testsite/venv/lib/python3.8/site-packages/flask_restplus/api.py", line 584, in error_router
    return original_handler(e)
  File "/Users/jyoseph/Sites/testsite/venv/lib/python3.8/site-packages/flask/app.py", line 1820, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/jyoseph/Sites/testsite/venv/lib/python3.8/site-packages/flask/_compat.py", line 38, in reraise
    raise value.with_traceback(tb)
  File "/Users/jyoseph/Sites/testsite/venv/lib/python3.8/site-packages/flask/app.py", line 1949, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Users/jyoseph/Sites/testsite/venv/lib/python3.8/site-packages/flask/app.py", line 1935, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/Users/jyoseph/Sites/testsite/venv/lib/python3.8/site-packages/flask_restplus/api.py", line 325, in wrapper
    resp = resource(*args, **kwargs)
  File "/Users/jyoseph/Sites/testsite/venv/lib/python3.8/site-packages/flask/views.py", line 89, in view
    return self.dispatch_request(*args, **kwargs)
  File "/Users/jyoseph/Sites/testsite/venv/lib/python3.8/site-packages/flask_restplus/resource.py", line 44, in dispatch_request
    resp = meth(*args, **kwargs)
  File "/Users/jyoseph/Sites/testsite/application/routes.py", line 31, in put
    Content.objects(content_id=idx).update(**data)
  File "/Users/jyoseph/Sites/testsite/venv/lib/python3.8/site-packages/mongoengine/queryset/base.py", line 521, in update
    update = transform.update(queryset._document, **update)
  File "/Users/jyoseph/Sites/testsite/venv/lib/python3.8/site-packages/mongoengine/queryset/transform.py", line 303, in update
    field = cleaned_fields[-1]
IndexError: list index out of range

我不知道是什么导致了这个问题。如果我更改代码以分别传入每个 属性,它工作正常...

    def put(self,idx):
        data = api.payload
        Content.objects(content_id=idx).update(content_id=data['content_id'], title=data['title'], excerpt=data['excerpt'], description=data['description'])
        return jsonify(Content.objects(content_id=idx))

但这并不理想。我希望使用 ** 来解压传入的数据对象。

您遇到的问题可能是由于 data 有一些不作为字段存在于对象中的附加键引起的。

在使用字典解包时,确保所有要解包到参数的键确实在您正在更新的 MongoDB 对象中。