Search code examples
pythonpython-3.xflaskpymongoflask-restplus

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


Flask==1.1.1 pymongo==3.10.1 flask-restplus==0.13.0

I'm attempting to PUT JSON to a URL and use update() but I'm running into an issue. In routes.py I have a simple API setup...

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

Using Postman I am PUTting to /api/content/2

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

And I receive this...

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

I can't figure out what may be causing this issue. If I change the code to pass in each property individually, it works fine...

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

But that is not ideal. I was hoping to use ** to unpack the data object that is passed in.


Solution

  • The issue you're having is likely caused by data having some additional key(s) that do not exist as fields in the object.

    While using dictionary unpacking, ensure that all of the keys that are going to be unpacked to arguments are indeed in the MongoDB object you are updating.