Home > Blockchain >  Update operation is not happening in FastAPI
Update operation is not happening in FastAPI

Time:06-30

@app.put("/blog/{id}", status_code=status.HTTP_202_ACCEPTED)
def update(id, request: schemas.Blog, db: Session = Depends(get_db)):
    db.query(models.Blog).filter(models.Blog.id == id).update(request)
    db.commit()
    return db

CodePudding user response:

Answer

@app.put("/blog/{id}", status_code=status.HTTP_202_ACCEPTED)
def update(id, request: schemas.Blog, db: Session = Depends(get_db)):
    db.query(models.Blog).filter(models.Blog.id == id).update(
        {"title": request.title, "body": request.body}
    )  # **passed each field in a dict**

    db.commit()

    return "Record updated successfully!"
  • Related