合剂 sql 炼金术获得 child object
Flask sql alchemy get child object
当我声明与 SQL-Alchemy 的关系时,当 Marshmallow 转储 object 时,我没有找到 child 的所有数据。当 Marshmallow 反序列化 object 时,我想让我的成员 object 变成 json 而不仅仅是 ID。
我的模特:
class Company(db.Model):
__table_args__ = {"schema": "public"}
id_company = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50), nullable=False)
members = db.relationship('Member', back_populates="company", lazy='joined')
class Member(db.Model):
__table_args__ = {"schema": "public"}
id_member = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(120), nullable=False)
age = db.Column(db.Integer)
company_id = db.Column(db.Integer, db.ForeignKey('public.company.id_company'), nullable=False)
company = db.relationship("Company", back_populates="members",lazy='joined')
class MemberSchema(ma.ModelSchema):
class Meta:
model = Member
class CompanySchema(ma.ModelSchema):
class Meta:
model = Company
class CompanyIdInputSchema(Schema):
id_company = fields.Integer(required=True)
我的路线:
company_schema = CompanySchema()
pagination_schema = PaginationInputSchema()
company_id_schema = CompanyIdInputSchema()
class CompanyById(Resource):
@swag_from('swag_docs/swag_company_get.yml')
def get(self, id_company):
company_id_args = {"id_company": id_company}
errors = company_id_schema.validate(company_id_args)
if errors:
return {"error": str(errors)}, 500
else:
company = Company.query.filter_by(id_company=id_company)
company = companies_schema.dump(company)
if len(company)>=1:
return company, 201
else:
return 204
调用路由时的结果是:
{
"members": [
1
],
"id_company": 1,
"name": "Total"
}
我的期望:
{
"members": [
{'id_member':1,'name':'Laurent','age':32,'company_id'=1}
],
"id_company": 1,
"name": "Total"
}
如果您希望序列化程序将嵌套字段序列化为序列化对象列表而不是 id,您需要添加 Nested
字段和 many=True
。
class CompanySchema(ma.ModelSchema):
members = ma.fields.Nested(MemberSchema, many=True)
class Meta:
model = Company
当我声明与 SQL-Alchemy 的关系时,当 Marshmallow 转储 object 时,我没有找到 child 的所有数据。当 Marshmallow 反序列化 object 时,我想让我的成员 object 变成 json 而不仅仅是 ID。
我的模特:
class Company(db.Model):
__table_args__ = {"schema": "public"}
id_company = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50), nullable=False)
members = db.relationship('Member', back_populates="company", lazy='joined')
class Member(db.Model):
__table_args__ = {"schema": "public"}
id_member = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(120), nullable=False)
age = db.Column(db.Integer)
company_id = db.Column(db.Integer, db.ForeignKey('public.company.id_company'), nullable=False)
company = db.relationship("Company", back_populates="members",lazy='joined')
class MemberSchema(ma.ModelSchema):
class Meta:
model = Member
class CompanySchema(ma.ModelSchema):
class Meta:
model = Company
class CompanyIdInputSchema(Schema):
id_company = fields.Integer(required=True)
我的路线:
company_schema = CompanySchema()
pagination_schema = PaginationInputSchema()
company_id_schema = CompanyIdInputSchema()
class CompanyById(Resource):
@swag_from('swag_docs/swag_company_get.yml')
def get(self, id_company):
company_id_args = {"id_company": id_company}
errors = company_id_schema.validate(company_id_args)
if errors:
return {"error": str(errors)}, 500
else:
company = Company.query.filter_by(id_company=id_company)
company = companies_schema.dump(company)
if len(company)>=1:
return company, 201
else:
return 204
调用路由时的结果是:
{
"members": [
1
],
"id_company": 1,
"name": "Total"
}
我的期望:
{
"members": [
{'id_member':1,'name':'Laurent','age':32,'company_id'=1}
],
"id_company": 1,
"name": "Total"
}
如果您希望序列化程序将嵌套字段序列化为序列化对象列表而不是 id,您需要添加 Nested
字段和 many=True
。
class CompanySchema(ma.ModelSchema):
members = ma.fields.Nested(MemberSchema, many=True)
class Meta:
model = Company