Mar*_*rty 1 python sqlalchemy flask marshmallow
我在如何从端点之一取回数据方面遇到了一些问题 - 特别是使用 Marshmallow 和 SQLAlchemy。
我在鸡尾酒和配料之间有多对多的关系,但我在关系表 ings_in_cocktail 上也有不仅仅是外键的更多数据,例如ounces.When I GET /cocktails/,它返回如下内容:
{
"cocktails": [
{
"glass": "rocks",
"ingredients": [
{
"ingredient": {
"ing_type": "liquor",
"id": 1,
"name": "gin"
},
"ounces": 20
}
],
"finish": "stirred",
"id": 1,
"name": "gin and tonic"
}
]
}
Run Code Online (Sandbox Code Playgroud)
我想做的是将财产的传播ounces与ingredient字典结合起来。
我希望数据如下所示:
{
"cocktails": [
{
"glass": "rocks",
"ingredients": [
{
"ing_type": "liquor",
"id": 1,
"name": "gin",
"ounces": 20
}
],
"finish": "stirred",
"id": 1,
"name": "gin and tonic"
}
]
}
Run Code Online (Sandbox Code Playgroud)
在网上搜索了几个小时后,我找不到使用 Marshmallow 轻松完成此操作的方法。我缺少一些简单的方法吗?
成分.py
from flask import Flask
from settings import db, ma
class Ingredient(db.Model):
__tablename__ = 'ingredients'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80), nullable=False)
ing_type = db.Column(db.String(20), nullable=False)
class IngredientSchema(ma.ModelSchema):
class Meta:
model = Ingredient
Run Code Online (Sandbox Code Playgroud)
ings_in_cocktail.py
from flask import Flask
from settings import db, ma
from models.ingredients import Ingredient, IngredientSchema
class CocktailIngredient(db.Model):
__tablename__ = 'ings_in_cocktail'
ing_id = db.Column(db.Integer, db.ForeignKey('ingredients.id'), primary_key=True)
cocktail_id = db.Column(db.Integer, db.ForeignKey('cocktails.id'), primary_key=True)
ounces = db.Column(db.Integer, nullable=False)
ingredient = db.relationship('Ingredient')
# Necessary for transforming sqlalchemy data into serialized JSON
class CocktailIngredientSchema(ma.ModelSchema):
ingredient = ma.Nested(IngredientSchema, strict=True)
class Meta:
model = CocktailIngredient
Run Code Online (Sandbox Code Playgroud)
鸡尾酒.py
from flask import Flask
from settings import db, ma
from models.ing_in_cocktails import CocktailIngredient, CocktailIngredientSchema
from models.ingredients import Ingredient, IngredientSchema
class Cocktail(db.Model):
__tablename__ = 'cocktails'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80), nullable=False)
glass = db.Column(db.String(20), nullable=False)
finish = db.Column(db.String(20), nullable=True)
ingredients = db.relationship(
'CocktailIngredient',
# secondary='ings_in_cocktail',
backref=db.backref('cocktails'),
# primaryjoin=id == CocktailIngredient.cocktail_id
)
# Necessary for transforming sqlalchemy data into serialized JSON
class CocktailSchema(ma.ModelSchema):
# this is responsible for returning all the ingredient data on the cocktail
ingredients = ma.Nested(CocktailIngredientSchema, many=True, strict=True)
class Meta:
model = Cocktail
Run Code Online (Sandbox Code Playgroud)
更新:我在底部放置了一个更好的解决方案。
您可以通过使用Method或Function字段来允许父级从其子级获取数据来实现此目的。这是您需要做的:
CocktailSchema了。 IngredientSchema(除非你还需要它做其他事情)。CocktailIngredientSchema,将该Nested字段替换为几个Function直接从内部“成分”对象中提取数据的字段。之前的模式
class IngredientSchema(ma.ModelSchema):
class Meta:
model = Ingredient
class CocktailIngredientSchema(ma.ModelSchema):
ingredient = ma.Nested(IngredientSchema)
class Meta:
model = CocktailIngredient
class CocktailSchema(ma.ModelSchema):
ingredients = ma.Nested(CocktailIngredientSchema, many=True)
class Meta:
model = Cocktail
Run Code Online (Sandbox Code Playgroud)
之后的模式
class CocktailIngredientSchema(ma.ModelSchema):
ing_type = ma.Function(lambda obj: obj.ingredient.ing_type)
id = ma.Function(lambda obj: obj.ingredient.id)
name = ma.Function(lambda obj: obj.ingredient.name)
class Meta:
model = CocktailIngredient
class CocktailSchema(ma.ModelSchema):
ingredients = ma.Nested(CocktailIngredientSchema, many=True)
class Meta:
model = Cocktail
Run Code Online (Sandbox Code Playgroud)
CocktailSchema了。 IngredientSchema会(而不是像上面所示的那样摆脱它)。CocktailIngredientSchema,将该Nested字段替换为几个Pluck直接从内部架构中提取数据的字段。之后的模式
class IngredientSchema(ma.ModelSchema):
class Meta:
model = Ingredient
class CocktailIngredientSchema(ma.ModelSchema):
ing_type = ma.Pluck(IngredientSchema, 'ing_type')
id = ma.Pluck(IngredientSchema, 'id')
name = ma.Pluck(IngredientSchema, 'name')
class Meta:
model = CocktailIngredient
class CocktailSchema(ma.ModelSchema):
ingredients = ma.Nested(CocktailIngredientSchema, many=True)
class Meta:
model = Cocktail
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2423 次 |
| 最近记录: |