I am trying to create a blog using Flask. Every post can have multiple tags also every tag could be associated with multiple posts. So I created a many-to-many relationship. My questions is how do i save multiple tags when creating a new post. And since every post can have different number of tags how do i show this is in the form? Also, how can i create new tags along with the post and then use those tags with other posts? This is models.py -
postcategory = db.Table('tags',
db.Column('posts_id', db.Integer, db.ForeignKey('posts.id')),
db.Column('categories_id', db.Integer, db.ForeignKey('categories.id'))
)
class Post(db.Model):
__tablename__ = 'posts'
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String)
content = db.Column(db.Text)
slug = db.Column(db.String, unique=True)
published = db.Column(db.Boolean, index=True)
timestamp = db.Column(db.DateTime, index=True)
categories = db.relationship('Category', secondary=postcategory, backref='posts' )
def __init__(self, title, content):
self.title = title
self.content = content
class Category(db.Model):
__tablename__ = 'categories'
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String, index=True)
This is the view i am working on -
def create_article():
if request.method == 'POST':
if request.form.get('title') and request.form.get('content') and request.form.get('slug') and request.form.get('published'):
post = Post(request.form['title'], request.form['content'], request.form['slug'], request.form['published'])
I am sure there is a easy solution and i am just complicating this, but i am new to web development, so please help.