'How to update database using flask and sqlalchemy?
I have created a list of users using Postgres (users table) and connected it to my flask backend app, while trying to update the username using postman I have a not found error. Want to understand what is not working. Here is the code I am trying to run:
from flask import Blueprint, jsonify, abort, request
from ..models import User
bp = Blueprint('users', __name__, url_prefix='/users')
@bp.route('/<int:id>', methods=['PATCH','PUT'])
def update(id: int):
u = User.query.get_or_404(id)
try:
if 'password' in request.json:
if len(request.json['username']) < 3:
return abort(404)
u.username = request.json['username']
# db.session.add(u)
db.session.commit()
return jsonify(u.serialize())
except:
# if something went wrong
return jsonify(description='Something went wrong')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
