0

我正在尝试返回所有用户信息,但每次运行请求时它都会返回一个空的 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

将用户添加到数据库时,密码将转换为纯字符串

4

1 回答 1

0

为了返回特定用户的所有详细信息,您可以使用以下代码:

def get_user(id):
    user = User.query.filter_by(id=id).first()
    if user is not None:
        response = jsonify({'result':user.to_json()})
    else
        response = jsonify({'message': 'cannot find user'}), 404
    return response

共享的代码有获取JSON格式的用户详细信息的详细信息。

于 2021-01-14T23:47:10.587 回答