Flask-sqlalchemy-Marshmallow 嵌套模式不起作用
Flask-sqlalchemy-Marshmallow nesting Schema not working
基本上我想做的是,加入 2 tables 'users' & 'company' 并让用户了解他们的相关公司详细信息。
这是用户模型:
class User(db.Model):
__tablename__ = 'user'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
firstname = db.Column(db.String(10), nullable=False)
lastname = db.Column(db.String(25), nullable=False)
username = db.Column(db.String(250), nullable=False)
email = db.Column(db.String(100), nullable=False)
password = db.Column(db.String(250), nullable=False)
isPasswordReset = db.Column(db.Boolean, nullable=False)
companyId = db.Column(db.Integer, db.ForeignKey(
'company.id'), nullable=True)
这是架构
class UserSchema(ma.Schema):
id = fields.Integer()
firstname = fields.String(required=True)
lastname = fields.String(required=True)
username = fields.String(required=True)
email = fields.String(required=True)
password = fields.String(required=True)
isPasswordReset = fields.Boolean(required=True)
companyId = fields.Integer()
company_name = fields.Nested(CompanySchema)
这是公司型号:
class Company(db.Model):
__tablename__ = 'company'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
companyName = db.Column(db.String(100), nullable=True)
companyCode = db.Column(db.String(20), nullable=True)
companyTheme = db.Column(db.String(300), nullable=True)
这是公司架构:
class CompanySchema(ma.Schema):
id = fields.Integer()
companyName = fields.String(required=True)
companyCode = fields.String(required=True)
companyTheme = fields.String(required=True)
这是资源-user.py :
users_schema = UserSchema(many=True)
user_schema = UserSchema()
class UsersResource(Resource):
def get(self):
users = db.session.query(User.firstname, Company.companyName).join(
Company, User.companyId == Company.id).all()
if users:
results = users_schema.dump(users).data
return {'status': 'success', 'message': json.dumps(results, default=str)}, 200
这是我得到的输出:
{
"status": "success",
"message": "[{\"firstname\": \"abc\"}, {\"firstname\": \"xyz\"}]"
}
仅显示用户 table,而不是来自公司的用户 table.i 已经学习了很多教程和东西几个小时了。但仍然无法修复 it.i 我是 Flask 和 sqlalchemy 的新手。请问有人知道如何纠正这个吗?
首先你需要在你的 sqlalchemy 模型中声明关系对象:
class User(db.Model):
__tablename__ = 'user'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
firstname = db.Column(db.String(10), nullable=False)
lastname = db.Column(db.String(25), nullable=False)
username = db.Column(db.String(250), nullable=False)
email = db.Column(db.String(100), nullable=False)
password = db.Column(db.String(250), nullable=False)
isPasswordReset = db.Column(db.Boolean, nullable=False)
companyId = db.Column(db.Integer, db.ForeignKey(
'company.id'), nullable=True)
company = db.relationship("Company", backref="parents") # <--
然后在用户模式中声明表示该对象的字段:
class UserSchema(ma.Schema):
id = fields.Integer()
firstname = fields.String(required=True)
lastname = fields.String(required=True)
username = fields.String(required=True)
email = fields.String(required=True)
password = fields.String(required=True)
isPasswordReset = fields.Boolean(required=True)
companyId = fields.Integer()
company = fields.Nested(CompanySchema) # <--
请注意,字段名称必须相同(或者您可以使用 attribute
参数)。
查询时你可以简单地做:
user_schema = UserSchema()
users = User.query.all()
results = users_schema.dump(users).data
return {'status': 'success', 'message': json.dumps(results, default=str)}, 200
(或 return users_schema.dump(users)
用于测试目的)
毕竟你应该得到这样的输出:
{
id: 'something',
firstname: 'something',
lastname: 'something',
username: 'something',
email: 'something',
password: 'something',
isPasswordReset: 'something',
company : {
id: 'something',
companyName: 'something',
companyCode: 'something',
companyTheme: 'something',
}
}
这能满足您的需求吗?
基本上我想做的是,加入 2 tables 'users' & 'company' 并让用户了解他们的相关公司详细信息。
这是用户模型:
class User(db.Model):
__tablename__ = 'user'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
firstname = db.Column(db.String(10), nullable=False)
lastname = db.Column(db.String(25), nullable=False)
username = db.Column(db.String(250), nullable=False)
email = db.Column(db.String(100), nullable=False)
password = db.Column(db.String(250), nullable=False)
isPasswordReset = db.Column(db.Boolean, nullable=False)
companyId = db.Column(db.Integer, db.ForeignKey(
'company.id'), nullable=True)
这是架构
class UserSchema(ma.Schema):
id = fields.Integer()
firstname = fields.String(required=True)
lastname = fields.String(required=True)
username = fields.String(required=True)
email = fields.String(required=True)
password = fields.String(required=True)
isPasswordReset = fields.Boolean(required=True)
companyId = fields.Integer()
company_name = fields.Nested(CompanySchema)
这是公司型号:
class Company(db.Model):
__tablename__ = 'company'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
companyName = db.Column(db.String(100), nullable=True)
companyCode = db.Column(db.String(20), nullable=True)
companyTheme = db.Column(db.String(300), nullable=True)
这是公司架构:
class CompanySchema(ma.Schema):
id = fields.Integer()
companyName = fields.String(required=True)
companyCode = fields.String(required=True)
companyTheme = fields.String(required=True)
这是资源-user.py :
users_schema = UserSchema(many=True)
user_schema = UserSchema()
class UsersResource(Resource):
def get(self):
users = db.session.query(User.firstname, Company.companyName).join(
Company, User.companyId == Company.id).all()
if users:
results = users_schema.dump(users).data
return {'status': 'success', 'message': json.dumps(results, default=str)}, 200
这是我得到的输出:
{ "status": "success", "message": "[{\"firstname\": \"abc\"}, {\"firstname\": \"xyz\"}]" }
仅显示用户 table,而不是来自公司的用户 table.i 已经学习了很多教程和东西几个小时了。但仍然无法修复 it.i 我是 Flask 和 sqlalchemy 的新手。请问有人知道如何纠正这个吗?
首先你需要在你的 sqlalchemy 模型中声明关系对象:
class User(db.Model):
__tablename__ = 'user'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
firstname = db.Column(db.String(10), nullable=False)
lastname = db.Column(db.String(25), nullable=False)
username = db.Column(db.String(250), nullable=False)
email = db.Column(db.String(100), nullable=False)
password = db.Column(db.String(250), nullable=False)
isPasswordReset = db.Column(db.Boolean, nullable=False)
companyId = db.Column(db.Integer, db.ForeignKey(
'company.id'), nullable=True)
company = db.relationship("Company", backref="parents") # <--
然后在用户模式中声明表示该对象的字段:
class UserSchema(ma.Schema):
id = fields.Integer()
firstname = fields.String(required=True)
lastname = fields.String(required=True)
username = fields.String(required=True)
email = fields.String(required=True)
password = fields.String(required=True)
isPasswordReset = fields.Boolean(required=True)
companyId = fields.Integer()
company = fields.Nested(CompanySchema) # <--
请注意,字段名称必须相同(或者您可以使用 attribute
参数)。
查询时你可以简单地做:
user_schema = UserSchema()
users = User.query.all()
results = users_schema.dump(users).data
return {'status': 'success', 'message': json.dumps(results, default=str)}, 200
(或 return users_schema.dump(users)
用于测试目的)
毕竟你应该得到这样的输出:
{
id: 'something',
firstname: 'something',
lastname: 'something',
username: 'something',
email: 'something',
password: 'something',
isPasswordReset: 'something',
company : {
id: 'something',
companyName: 'something',
companyCode: 'something',
companyTheme: 'something',
}
}
这能满足您的需求吗?