title: Many-to-one Flask SqlAlchemy rendering
body: <p>I'm trying to create categories for my posts. End result should be a user when trying to post, they choose a category, the post goes under that category so it can be indexed there. So for example they'd go on example.com/category/Flask and they'd find all the posts that have the Flask category. I added to the post model a category_id and category and then I created a Category model with an id and a name. Then created a form so I can add categories to the category table so users can start choosing categories for their posts. What has worked for me is that I can create categories, choose them when posting, and when I look at the database tables I can see that those posts have categories assigned to them. So what I want to do now is to create a view for /category/, and I have no idea how to do that. </p>
  
  <pre><code>class Post(db.Model):
      __tablename__ = 'posts'
      id = db.Column(db.Integer, primary_key=True)
      title = db.Column(db.String(80))
      body = db.Column(db.Text)
      timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow)
      author_id = db.Column(db.Integer, db.ForeignKey('users.id'))
      body_html = db.Column(db.Text)
      comments = db.relationship('Comment', backref='post', lazy='dynamic')
      category_id = db.Column(db.Integer, db.ForeignKey('categories.id'))
      category = db.relationship('Category',
                                 backref=db.backref('posts', lazy='dynamic'))
  
  
      @staticmethod
      def on_changed_body(target, value, oldvalue, initiator):
          allowed_tags = ['a', 'abbr', 'acronym', 'b', 'blackquote', 'code', 'em', 'i', 'li', 'ol', 'pre', 'strong',
                          'ul', 'h1', 'h2', 'h3', 'p']
          target.body_html = bleach.linkify(bleach.clean(
              markdown(value, output_form='html'),
              tags=allowed_tags, strip=True))
  
  db.event.listen(Post.body, 'set', Post.on_changed_body)
  
  
  class Category(db.Model):
      __tablename__ = 'categories'
      id = db.Column(db.Integer, primary_key=True)
      name = db.Column(db.String(50))
  
      def __repr__(self):
          return '&lt;Category {}&gt;'.format(self.name)
  </code></pre>
  
  <p>I created a CategoryForm</p>
  
  <pre><code>class CategoryForm(Form):
      name = StringField('Category name', validators=[DataRequired()])
      submit = SubmitField('Submit')
  </code></pre>
  
  <p>Category.html</p>
  
  <pre><code>{% extends "base.html" %}
  {% import "bootstrap/wtf.html" as wtf %}
  {% import "_macros.html" as macros %}
  
  {% block title %}Flasky{% endblock %}
  
  {% block page_content %}
  
  {{ wtf.quick_form(form) }}
  
  {% endblock %}
  </code></pre>
  
  <p>Category view</p>
  
  <pre><code>@main.route('/add_category', methods=['GET', 'POST'])
  def add_category():
      form = CategoryForm()
      if form.validate_on_submit():
          category = Category(name=form.name.data)
          db.session.add(category)
          flash('A confirmation email jas been sent you.')
          return redirect(url_for('main.index'))
      return render_template('add_category.html', form=form)
  </code></pre>
  
  <p>and then after adding categories I edited the PostForm with</p>
  
  <pre><code>def enabled_categories():
      return Category.query.all()
  
  
  class PostForm(Form):
      body = PageDownField("What's on your mind?", validators=[DataRequired()])
      category = QuerySelectField('Category', query_factory=enabled_categories,
                                  allow_blank=True)
      submit = SubmitField('Submit')
  </code></pre>
  
  <p>And this is the route I have created.</p>
  
  <pre><code>@main.route('/category/&lt;name&gt;')
  def view_posts_in_category(name):
      name = Category.query.filter_by(name=name)
      return render_template('category.html', name=name)
  </code></pre>
  
  <p>So now in the category.html 'category/Flask' how can I show all the posts under the Flask category? e.g {{ category.post }} ? What should I use?</p>