1

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.

jyoseph
  • 5,435
  • 9
  • 45
  • 64
  • Does `data` contain only the four keys that you are passing individually (i.e. `content_id`, `title`, `excerpt`, `description`)? – Arn Mar 05 '20 at 20:21
  • @Arn Oh man, this seemed to be the problem. I was passing in another key that did not exist in the mongodb object I was updating. e.g. ``` { "content_id": 2, "title": "Test 2", "description": "Test 2", "type": "Test 2" } ``` Seems like adding that additional field caused the error. If you add this as an answer I'm happy to accept it. Thanks for the hand! – jyoseph Mar 05 '20 at 23:21
  • No worries, I've just posted it. – Arn Mar 06 '20 at 10:36

1 Answers1

1

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.

Arn
  • 1,898
  • 12
  • 26