title: Jsonify flask-sqlalchemy many-to-one relationship in flask
body: <p>I was trying to make rest apis with flask-restful where i am using flask-sqlalchemy as the ORM.Here is my model classes.</p>
  
  <pre><code>class Post(db.Model):
  __tablename__ = 'post'
  postid = db.Column(db.Integer,primary_key=True)
  post = db.Column(db.String(64))
  userid = db.Column(db.Integer,db.ForeignKey('user.userid'))
  
  #serialize property used for serializing this class to JSON
  @property
  def serialize(self):
      return {
          'postid': self.postid,
          'post': self.post,
          'author':self.author
      }
  </code></pre>
  
  <p>and </p>
  
  <pre><code>class User(db.Model):
  userid = db.Column(db.Integer,primary_key=True)
  username = db.Column(db.String(30))
  email = db.Column(db.String(20))
  posts = db.relationship('Post',backref="author",lazy='dynamic')
  
  #serialize property used for serializing this class to JSON
  @property
  def serialize(self):
      return {
          'username': self.username,
          'email': self.email
      }
  </code></pre>
  
  <p>And the database is populated.now i am trying to make the json out of this</p>
  
  <pre><code>class PostList(Resource):
      def get(self):
          posts = DL_models.Post.query.all()
          posts = [post.serialize for post in posts]
          return { 'posts': posts }
  
  api.add_resource(PostList, '/twitter/api/v1.0/posts', endpoint = 'posts')
  </code></pre>
  
  <p>This works perfect when i change serialize method in Post to </p>
  
  <pre><code>@property
  def serialize(self):
      return {
          'postid': self.postid,
          'post': self.post,
          'author':self.postid
      }
  </code></pre>
  
  <p>This returns expected json output but when i am changing to <code>'author':self.author</code> i am getting a error</p>
  
  <pre><code>TypeError: &lt;app.DL_models.User object at 0x7f263f3adc10&gt; is not JSON serializable
  </code></pre>
  
  <p>I understand that i have to call serialize on those nested objects as well but i could not figure out how to do it.</p>
  
  <p>Or please share your experience to encode relationships in sqlalchemy.</p>