我正在使用flask-marshmallow
。
作为回应,我得到下一个类似的json
:
data = {
'id': '1.0.1',
'name': 'test',
'applicaion_id': 'google',
}
如何从
application.name
获取Application
?反应
class VersionDetail extends Component {
state = {
app: {
id: '',
name: '',
application_id: '',
}
}
componentDidMount() {
axios.get('/apps/'+this.props.match.params.id)
.then(response => this.setState({ app: response.data }))
.catch(function (error) {
})
}
render() {
return ()
}
}
路线
class ApplicationDetailSchema(ma.ModelSchema):
class Meta:
model = Application
fields = ('id', 'name', 'versions')
class VersionDetailSchema(ma.ModelSchema):
class Meta:
model = Version
fields = ('id', 'file', 'application_id')
version_schema = VersionDetailSchema()
@app.route("/<application_id>/<version_id>")
def version_detail(id):
application = Application.get(application_id)
version = Version.get(version_id)
return version_schema.dump(version)
楷模
class Application(db.Model):
__tablename__ = 'applications'
id = db.Column(db.String(), primary_key=True)
name = db.Column(db.String())
versions = db.relationship('Version', backref='application', lazy=True)
def __repr__(self):
return '<application {}>'.format(self.name)
class Version(db.Model):
__tablename__ = 'versions'
id = db.Column(db.String(), primary_key=True)
file = db.Column(db.String(80), nullable=True)
application_id = db.Column(db.Integer, db.ForeignKey('applications.id'))
def __repr__(self):
return '<version {}>'.format(self.id)
最佳答案
我认为您需要在ma.Nested
中添加VersionDetailSchema
架构,如this answer所述-类似;
class VersionDetailSchema(ma.ModelSchema):
application = ma.Nested(ApplicationDetailsSchema, only=['name'])
class Meta:
model = Version
fields = ('id', 'file', 'application_id', 'application')
我只是猜测基于marshmallow docs的
only=['name']
不幸的是,关于
flask-marshmallow
的文档记录方式不多-我个人发现,使用该附件的好处实际上比记录得更好的marshmallow
本身要少-设置几乎不是很难。关于reactjs - 如何从关系模型中获取其他字段?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58401965/