title: How to enter value for foriegn key field via sqlalchemy
body: <p>I have models:</p>
  
  <pre><code>class Post(db.Model):
      id = db.Column(db.Integer, primary_key = True)
      body = db.Column(db.String(2000))
      user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
      comments = db.relationship('Comment', backref='parent_post', lazy='dynamic')
  
  
  class Comment(db.Model):
      id = db.Column(db.Integer, primary_key = True)
      body = db.Column(db.String(140))
      post_id = db.Column(db.Integer, db.ForeignKey('post.id'))
  </code></pre>
  
  <p>When entering a post to database I do this:</p>
  
  <pre><code>if form.validate_on_submit():
        post = Post(body=form.post.data, author=g.user)
        db.session.add(post)
        db.session.commit()
  </code></pre>
  
  <p>This is working right.</p>
  
  <p>But how can I enter a comment to database if I want to pass the 'post.id' value directly 
  instead of object 'post'. (Not able to 'pass' object via form in html)</p>
  
  <pre><code>if form.validate_on_submit():
        comment = Comment(body=form.post.data, parent_post=form.p_id.data)
        db.session.add(comment)
        db.session.commit()
  </code></pre>
  
  <p>currently <code>p_id</code> holds value <code>post.id</code> and it gives me error:</p>
  
  <pre><code>AttributeError: 'int' object has no attribute '_sa_instance_state'
  </code></pre>