The rule is to use the class you created NestedSchema and not nested_schema, do the following:
result,errors = NestedSchema().dump({'listing':listing,'location':location})
And the result will be as follows:
dict: { u'listing': {u'id': 8, u'title': u'foo'}, u'location': {u'id': 30, u'title': u'bar'} }
But I donβt understand why you wanted to create "NestedSchema", I think you can do it differently.
First forget the "NestedSchema" class.
After that, change your "ListingSchema", for example:
class ListingSchema(Schema): id = fields.Int() title = fields.Str() location_id = fields.Int() location = fields.Nested("LocationSchema")
And now you can do:
listing = db.session.query(Listing).get(listing_id)
The result will be:
dict: { u'id': 8, u'title': u'foo', u'location_id': 30, u'location': {u'id': 30, u'title': u'bar'} }
Note that now "location" is a "listing" property.
And you can still make a Bilateral attachment , just add backref to the Listing (model) and add ListingSchema as nested in LocationSchema
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")
many=True is that we have a one-to-many relationship. exclude=("location") should avoid recursion exclusion.
Now we can search by location.
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'}, ... ] }
You can see the docs about it here.