Search code examples
pythonapiputmongoengine

PUT-api: How do I addtoset a particular json into a child-array through python / mongoengine


I am stuck on the following, I have a put-call designed:

   def put(self) -> Response:

        data = request.get_json()
        print(data)
        list_id = data['id']
        print(list_id)
        sublist = data['sublist']
        print(sublist)
        put_list = Lists.objects(id=list_id).update({"$addToSet":  { "$sublist": sublist }})
        return jsonify({'result': put_list})

Which reaches my database, prints the values I would like to use correctly. However I still receive the error:

ValueError: update cannot be empty

Feels like my syntax in particularly this line does not add up:

        put_list = Lists.objects(id=list_id).update({"$addToSet":  { "$sublist": sublist }})

What could be the solution here?

Would love to add another sublist json underneath the current one which is part of list.

Hope you are able to help me / thanks!!


Solution

  • I fixed it after some trial and error / googling with:

    put_list = Lists.objects(id=list_id).update_one(add_to_set__sublist=sublist)