1

I'm use flask-marshmallow.

In response I get next simillary json:

data = {
    'id': '1.0.1',
    'name': 'test',
    'applicaion_id': 'google',
}

How can I get application.name from Application?

react

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 ()
  }
}

routes

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)

models

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)
unknown
  • 252
  • 3
  • 12
  • 37

1 Answers1

1

I think you'll need to add a ma.Nested schema into your VersionDetailSchema, as outlined in this answer - something like;

class VersionDetailSchema(ma.ModelSchema):

  application = ma.Nested(ApplicationDetailsSchema, only=['name'])

  class Meta:
    model = Version
    fields = ('id', 'file', 'application_id', 'application')

I'm only guessing at the only=['name'] based off the marshmallow docs

There is unfortunately not much in the way of documentation on this for flask-marshmallow - I personally find the benefit of using that add on is actually less than the more well documented marshmallow by itself - the setup is hardly very difficult.

unknown
  • 252
  • 3
  • 12
  • 37
elembie
  • 600
  • 2
  • 8