title: Combine Flask-Marshmallow with marshmallow-jsonapi
body: <h1>Overview</h1>
  
  <p>I am using Flask-SqlAlchemy and now I am looking into marshmallow to help me serialize and deserialize request data.</p>
  
  <p>I was able to successfully:</p>
  
  <ol>
  <li><p>Create my models using <strong>Flask-SqlAlchemy</strong></p></li>
  <li><p>Use <strong>Flask-Marshmallow</strong> to serialize database objects using the same model, by using the <a href="https://flask-marshmallow.readthedocs.io/en/latest/#optional-flask-sqlalchemy-integration" rel="nofollow noreferrer">Optional Flask-SqlAlchemy Integration</a></p></li>
  <li><p>Use <strong>marshmallow-jsonapi</strong> to quickly generate Json API compliant responses. This required me to <a href="https://marshmallow-jsonapi.readthedocs.io/en/latest/quickstart.html#declaring-schemas" rel="nofollow noreferrer">declare new Schemas</a> to specify which attributes  I want to include (this is duplicate from Flask-SqlAlchemy Models)</p></li>
  </ol>
  
  <h1>Code Samples</h1>
  
  <h3>Flask-SqlAlchemy Declarative Model</h3>
  
  <pre><code>class Space(db.Model):
  
      __tablename__ = 'spaces'
      id = sql.Column(sql.Integer, primary_key=True)
      name = sql.Column(sql.String)
      version = sql.Column(sql.String)
      active = sql.Column(sql.Boolean)
  </code></pre>
  
  <h3>flask_marshmallow Schema Declaration (Inherits from SqlAlchemy Model)</h3>
  
  <pre><code>ma = flask_marshmallow.Marshmallow(app)
  
  class SpaceSchema(ma.ModelSchema):
      class Meta:
          model = Space
  
  # API Response
  space = Space.query.first()
  return SpaceSchema().dump(space).data
  
  # Returns:
  {
  'id': 123,
  'version': '0.1.0',
  'name': 'SpaceName',
  'active': True
  }
  </code></pre>
  
  <h3>marshmallow_json api - requires new Schema Declaration, must include each attribute and type manually</h3>
  
  <pre><code>class SpaceJsonSchema(marshmallow_json.Schema):
      id = fields.Str(dump_only=True)
      name = fields.Str()
      version = fields.Str()
      active = fields.Bool()
  
      class Meta:
          type_ = 'spaces'
          self_url = '/spaces/{id}'
          self_url_kwargs = {'id': '&lt;id&gt;'}
          self_url_many = '/spaces/'
          strict = True
  
  # Returns Json API Compliant
  {
      'data': {
          'id': '1',
          'type': 'spaces',
          'attributes': {
              'name': 'Phonebooth',
              'active': True,
              'version': '0.1.0'
           },
          'links': {'self': '/spaces/1'}
      },
      'links': {'self': '/spaces/1'}
  }
  </code></pre>
  
  <h1>Issue</h1>
  
  <p>As shown in the code, <strong>marshmallow-jsonapi</strong> allows me to create json api compliant responses, but I end up having to maintain a Declarative Model + Schema Response model.</p>
  
  <p><strong>flask-marshmallow</strong> allows me to create Schema responses from the SqlAlchemy models, so I don't have to maintain a separate set of properties for each model.</p>
  
  <hr>
  
  <h2>Question</h2>
  
  <p>Is it at all possible to use <strong>flask-marshmallow</strong> and <strong>marshmallow-jsonapi</strong> together so 1. Create Marshmallow Schema from a SqlAlchemy model, <strong>AND</strong> automatically generate json api responses?</p>
  
  <p>I tried creating  Schema declaration that inherited from <code>ma.ModelSchema</code> and <code>marshmallow_json.Schema</code>, in both orders, but it does not work (raises exception for missing methods and properties)</p>
  
  <hr>
  
  <h3><a href="https://marshmallow-jsonapi.readthedocs.io/en/latest/index.html" rel="nofollow noreferrer">marshmallow-jsonapi</a></h3>
  
  <blockquote>
    <p>marshmallow-jsonapi provides a simple way to produce JSON
    API-compliant data in any Python web framework.</p>
  </blockquote>
  
  <h3><a href="https://flask-marshmallow.readthedocs.io/en/latest/" rel="nofollow noreferrer">flask-marshmallow</a></h3>
  
  <blockquote>
    <p>Flask-Marshmallow includes useful extras for integrating with
    Flask-SQLAlchemy and marshmallow-sqlalchemy.</p>
  </blockquote>