title: How to get Flask-SQLAlchemy object to load relationship children for Jinja template?
body: <p>I have basic models for User and Post. In my User model, I have</p>
  
  <pre><code>posts = db.relationship('Post', backref='user', lazy='dynamic')
  </code></pre>
  
  <p>However, when I do something like</p>
  
  <pre><code>return render_template('user.html', users=users)
  </code></pre>
  
  <p>I want to do stuff like</p>
  
  <pre><code>{% for user in users %}
      &lt;tr&gt;
          &lt;td&gt;{{ user.id }}&lt;/td&gt;
          &lt;td&gt;{{ user.posts|length }}&lt;/td&gt;
      &lt;/tr&gt;
  {% endfor %}
  </code></pre>
  
  <p>Unfortunately, this doesn't work. Posts is a query, not an object b/c of <code>lazy='dynamic'</code>. I can do the above if I change <code>lazy='joined'</code>, but then it would be loading all posts for users anytime I query for a User.</p>
  
  <p>I tried adding <code>.options(joinedload('posts'))</code> to my query, but it said that </p>
  
  <blockquote>
    <p>InvalidRequestError: 'User.posts' does not support object population - eager loading cannot be applied.</p>
  </blockquote>
  
  <p>Any help is appreciated.</p>