Serialize two nested schema with marshmallow
<p>I am fairly new to python. I have two SQLAlchemy models as follows:</p>

<pre><code>class listing(db.Model):
 id = db.Integer(primary_key=True)
 title = db.String()
 location_id = db.Column(db.Integer, db.ForeignKey('location.id'))
 location = db.relationship('Location', lazy='joined')

class location(db.Model):
 id = db.Integer(primary_key=True)
 title = db.String()
</code></pre>

<p>I have two Marshmallow schema classes for them:</p>

<pre><code>class ListingSchema(Schema):
 id = fields.Int()
 title = fields.Str()
 location_id = fields.Int()

class LocationSchema(Schema):
 id = fields.Int()
 title = fields.Str()
</code></pre>

<p>I have created a nested schema class like:</p>

<pre><code>class NestedSchema(Schema):
 listing = fields.Nested(ListingSchema)
 location fields.Nested(LocationSchema)
</code></pre>

<p>I am doing the join query like :</p>

<pre><code>listing,location = db.session.query(Listing,Location)\
                            .join(Location, and_(Listing.location_id == Location.id))\
                            .filter(Listing.id == listing_id).first()
</code></pre>

<p>data gets load in the objects i have checked. How can parse this schema?
I have tried</p>

<pre><code>result,errors = nested_listing_Schema(listing,location)
</code></pre>

<p>This gives error: "Listing object is not iteratable."</p>

<p>The right is to use the class that you created NestedSchema and not nested_schema, do this:</p>

<pre><code>result,errors = NestedSchema().dump({'listing':listing,'location':location})
</code></pre>

<p>And the result wiil be:</p>

<pre><code>dict: {
       u'listing': {u'id': 8, u'title': u'foo'},
       u'location': {u'id': 30, u'title': u'bar'}
      }
</code></pre>

<p>But i do not understand why you wanted to make a "NestedSchema", i think you can do it another way.</p>

<p>First, forget the class "NestedSchema".</p>

<p>After, change your "ListingSchema", like this:</p>

<pre><code>class ListingSchema(Schema):
    id = fields.Int()
    title = fields.Str()
    location_id = fields.Int()
    location = fields.Nested("LocationSchema") #The diff is here
</code></pre>

<p>And now you can do:</p>

<pre><code>listing = db.session.query(Listing).get(listing_id) # Suppose listing_id = 8
result,errors = ListingSchema().dump(listing)
print result
</code></pre>

<p>The result will be:</p>

<pre><code>dict: {
        u'id': 8, u'title': u'foo', u'location_id': 30, u'location': {u'id': 30, u'title': u'bar'}
      }
</code></pre>

<p>Note that now, "location" is a property of "listing".</p>

<p>And you can still make a <a href="http://marshmallow.readthedocs.io/en/latest/nesting.html#two-way-nesting" rel="nofollow">Two-Way Nesting</a>, just add a backref in Listing (model) and add ListingSchema as nested in LocationSchema</p>

<pre><code>class Listing(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(45), nullable=False)
    location_id = db.Column(db.Integer, db.ForeignKey('location.id'))
    location = db.relationship('Location', lazy='joined', backref="listings") #the diff is here

class LocationSchema(Schema):
    id = fields.Int()
    title = fields.Str()
    listings = fields.Nested("ListingSchema", many=True, exclude=("location",)) #The property name is the same as in bakcref
</code></pre>

<p>The <code>many=True</code> is because we have a relationship One-to-Many.
The <code>exclude=("location")</code> is to avoid recursion exception.</p>

<p>Now we can search by location too.</p>

<pre><code>location = db.session.query(Location).get(location_id) # Suppose location_id = 30
result,errors = LocationSchema().dump(location)
print result

dict: {u'id': 30, u'title': u'bar', 
       u'listings': [
             {u'location_id': 30, u'id': 8, u'title': u'foo'},
             {u'location_id': 30, u'id': 9, u'title': u'foo bar baz'},
             ...
             ]
       }
</code></pre>

<p>You can see the docs about it <a href="http://marshmallow.readthedocs.io/en/latest/nesting.html" rel="nofollow">here</a></p>

python flask-sqlalchemy marshmallow