Flask-SQLalchemy update a row's information

前端 未结 5 2120
南方客
南方客 2020-12-02 03:59

How can I update a row\'s information?

For example I\'d like to alter the name column of the row that has the id 5.

5条回答
  •  死守一世寂寞
    2020-12-02 04:49

    In RestApi, We can update the record dynamically by passing the json data into update query:

    class UpdateUserDetails(Resource):
    @auth_token_required
    def post(self):
        json_data = request.get_json()
        user_id = current_user.id
        try:
            userdata = User.query.filter(User.id==user_id).update(dict(json_data))
            db.session.commit()
            msg={"msg":"User details updated successfully"}
            code=200
        except Exception as e:
            print(e)
            msg = {"msg": "Failed to update the userdetails! please contact your administartor."}
            code=500
        return msg
    

提交回复
热议问题