title: Access related records using flask-sqlalchemy
body: <p>I am trying to access all related records using flask-sqlalchemy.</p>
  
  <p>My model class' have the following structure: </p>
  
  <pre><code>class User(db.Model):
      id = db.Column(db.Integer, primary_key=True)
      name = db.Column(db.String(64), index=True, nullable=False)
      posts = db.relationship('Post', backref='author', lazy='dynamic')
  
      def __repr__(self):
          return f'&lt;User {self.name}&gt;'
  
  
  class Post(db.Model):
      id = db.Column(db.Integer, primary_key=True)
      title = db.Column(db.String(64), index=True, nullable=False)
      date_posted = db.Column(db.DateTime, index=True, default=datetime.utcnow)
      department_id = db.Column(db.Integer, db.ForeignKey('user.id'))
  
      def __repr__(self):
          return f'&lt;Post {self.title}&gt;'
  </code></pre>
  
  <p>I would like to access Post information related to a particular User;
  The following template is passed a copy of the Users record.</p>
  
  <pre><code>{% if users %}
  &lt;p&gt;USERS&lt;/p&gt;
      &lt;ul&gt;
          {% for user in users %}
              &lt;li&gt; {{ user.id }}&lt;/li&gt;
              &lt;li&gt; {{ user.name }}&lt;/li&gt;
  
          {% for post in user.posts %}
              &lt;li&gt;{{ post.title }} &lt;/li&gt;
          {% endfor %}
  
          {% endfor %}
      &lt;/ul&gt;
  {% endif %}
  </code></pre>
  
  <p>Do I need to create some kind of dict to map posts to their uers before passing the values to the template? </p>
  
  <p>I appreciate the simplicity of this question, I am an absolute beginner. Any help would be very much appreciated.</p>