我正在尝试返回所有用户信息,但每次运行请求时它都会返回一个空的 JSON 对象
我的模型和架构:
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
db = SQLAlchemy()
ma = Marshmallow()
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String)
email = db.Column(db.String(100), unique=True)
password = db.Column(db.String(100), unique=True)
def __init__(self, name, email, password):
self.name = name
self.email = email
self.password = password
class UserSchema(ma.Schema):
class meta:
fields = ('id', 'name', 'email', 'password')
user_schema = UserSchema()
获取函数:
@auth.route('/getuser/<id>', methods=['GET'])
def get_user(id):
password = request.json['password']
user = User.query.get(id)
if not user or not bcrypt.checkpw(password.encode('utf-8'), user.password.encode('utf-8')):
return jsonify({'msg': 'Wrong email or password'})
result = user_schema.dump(user)
return result
将用户添加到数据库时,密码将转换为纯字符串