Home > Back-end >  The flask api also updates the put method but does not save it to the database
The flask api also updates the put method but does not save it to the database

Time:06-29

I have a flask api project. I want to use the User/Delete method to save the isdeleted fields = true in the database. While trying the put method in the postman, it updates it and it shows isdeleted=true, but it doesn't save it to the database. And when I get it, it returns "false".

@app.route('/User/Delete', methods=['PUT'])
@token_required
def delete_users(current_user):
    if 'id' in request.headers:
        id = request.headers['id']
        user = Users.query.get(id)
        user.isdeleted = True
    db.session.commit()
    return user_schema.jsonify(user)

CodePudding user response:

I didn't get your question properly but I think you are trying to ensure that the value for isdeleted is added to your database. If so, before db.session.commit(), add db.session.add(some_object). Your code should look like this:

@app.route('/User/Delete', methods=['PUT'])
@token_required
def delete_users(current_user):
    if 'id' in request.headers:
        id = request.headers['id']
        user = Users.query.get(id)
        user.isdeleted = True
        deleted_id=user.isdeleted
    db.session.add(deleted_id)
    db.session.commit()
    return user_schema.jsonify(user)
  • Related