marshmallow-sqlalchemy 无法从父 model_schema 获取子模型的数据
marshmallow-sqlalchemy Unable to get child model's data from parent model_schema
我正在尝试使用 marshmallow-sqlalchemy 创建一个 API
但是,当我转储数据时,它只显示父模型数据
赞 {'id': 1, 'date': '2019-09-01'}
如何获取公司数据,例如 {'id': 1, 'date': '2019-09-01', 'orders': {'id': 1, 'date': '2019-09-01'}, {'id': 1, 'date': '2019-09-01'}}
模型是
class Order(db.Model):
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
company_id = db.Column(db.Integer, db.ForeignKey('user_company.id'), nullable=False)
company = db.relationship('UserCompany', backref='orders', lazy=True)
date = db.Column(db.DateTime(), server_default=func.now())
class UserCompany(db.Model):
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
company_name = db.Column(db.String(length=20))
模式是
class OrderSchema(ModelSchema):
class Meta:
sqla_session = db.Session
fields = ('id', 'date')
model = Order
order_schema = OrderSchema()
orders_schema = OrderSchema(many=True)
class UserCompanySchema(ModelSchema):
orders = Nested(OrderSchema, many=True)
class Meta:
fields = ('id', 'company_name')
model = UserCompany
sqla_session = db.session
user_company_schema = UserCompanySchema()
视图是
company = UserCompany.query.get(current_user.company.id)
print(user_company_schema.dump(company))
{'id': 1, 'date': '2019-09-01'}
我要的是
{'id': 1, 'date': '2019-09-01', 'orders': {'id': 1, 'date': '2019-09-01'}, {'id': 1, 'date': '2019-09-01'}}
如果您希望它们出现在转储中,您需要在 Meta class 中明确设置所有需要的字段。
class UserCompanySchema(ModelSchema):
orders = Nested(OrderSchema, many=True)
class Meta:
fields = ('id', 'company_name', 'orders')
model = UserCompany
sqla_session = db.session
我正在尝试使用 marshmallow-sqlalchemy 创建一个 API
但是,当我转储数据时,它只显示父模型数据
赞 {'id': 1, 'date': '2019-09-01'}
如何获取公司数据,例如 {'id': 1, 'date': '2019-09-01', 'orders': {'id': 1, 'date': '2019-09-01'}, {'id': 1, 'date': '2019-09-01'}}
模型是
class Order(db.Model):
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
company_id = db.Column(db.Integer, db.ForeignKey('user_company.id'), nullable=False)
company = db.relationship('UserCompany', backref='orders', lazy=True)
date = db.Column(db.DateTime(), server_default=func.now())
class UserCompany(db.Model):
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
company_name = db.Column(db.String(length=20))
模式是
class OrderSchema(ModelSchema):
class Meta:
sqla_session = db.Session
fields = ('id', 'date')
model = Order
order_schema = OrderSchema()
orders_schema = OrderSchema(many=True)
class UserCompanySchema(ModelSchema):
orders = Nested(OrderSchema, many=True)
class Meta:
fields = ('id', 'company_name')
model = UserCompany
sqla_session = db.session
user_company_schema = UserCompanySchema()
视图是
company = UserCompany.query.get(current_user.company.id)
print(user_company_schema.dump(company))
{'id': 1, 'date': '2019-09-01'}
我要的是
{'id': 1, 'date': '2019-09-01', 'orders': {'id': 1, 'date': '2019-09-01'}, {'id': 1, 'date': '2019-09-01'}}
如果您希望它们出现在转储中,您需要在 Meta class 中明确设置所有需要的字段。
class UserCompanySchema(ModelSchema):
orders = Nested(OrderSchema, many=True)
class Meta:
fields = ('id', 'company_name', 'orders')
model = UserCompany
sqla_session = db.session