I use SQLalchemy with a many to many table to manage blog post tags. I need help rendering the tag values into a TextArea form field where they can be edited. Right now when I render I see the lookup query.
Model
The relationship between Tag and Post is is defined in `tags'
class Tag(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50), unique=True)
url = db.Column(db.String(120), unique=True)
def __init__(self, name, url):
self.name = name
self.url = url
class Post(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(80))
body = db.Column(db.Text)
pub_date = db.Column(db.DateTime)
tags = db.relationship('Tag', secondary=posts_tags, backref='posts', lazy='dynamic')
class Tag(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50), unique=True)
url = db.Column(db.String(120), unique=True)
My problem is rendering the tags field in WTF. I displays the query in the field instead of the results.
I see two options to fix, but I don't know how to do either....
a.) In the view, interate through tags and display the values. b.) In a custom field, somehow pass the post id and run the a query before before rending the field. Something like, this....
Field Hack That works, but know how to dynamically pass the Post ID to the field.*
class TagListField(Field):
widget = TextArea()
def _value(self):
q = Post.query.join(posts_tags, (posts_tags.c.post_id == {{ NEED HELP HERE}}))
taglist = []
for p in q:
for t in p.tags:
taglist.append(t.name)
taglist
return ", ".join(taglist)
def process_formdata(self, valuelist):
if valuelist:
self.data = [x.strip() for x in valuelist[0].split(',')]
else:
self.data = []
Would like to see view and field options... Thanks and advance...