[英]How to send query results to a WTForm Field?
I use SQLalchemy with a many to many table to manage blog post tags. 我使用带有多对多表的SQLalchemy来管理博客帖子标签。 I need help rendering the tag values into a TextArea form field where they can be edited. 我需要帮助将标记值呈现到TextArea表单字段中,以便对其进行编辑。 Right now when I render I see the lookup query. 现在,当我渲染时,我看到了查询查询。
Model 模型
The relationship between Tag and Post is is defined in `tags' Tag和Post之间的关系在`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. 我的问题是在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. a。)在视图中,通过标签进行交互并显示值。 b.) In a custom field, somehow pass the post id and run the a query before before rending the field. b。)在自定义字段中,以某种方式传递post id并在rend字段之前运行查询。 Something like, this.... 像这样......
Field Hack That works, but know how to dynamically pass the Post ID to the field.* Field Hack可行,但知道如何动态地将Post ID传递给该字段。*
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... 希望看到视图和字段选项......谢谢并提前...
You are using _value incorrectly, it's used to display the data, not to set the data. 您正在错误地使用_value,它用于显示数据,而不是用于设置数据。
The data is set on the Form, not on the Field. 数据在表单上设置,而不是在字段上设置。
class TagListField(Field):
widget = TextInput()
def _value(self):
if self.data:
return u', '.join(self.data)
else:
return u''
def process_formdata(self, valuelist):
if valuelist:
self.data = [x.strip() for x in valuelist[0].split(',')]
else:
self.data = []
class PostForm(Form):
title = StringField(u'title', validators=[DataRequired()])
body = StringField(u'Text', widget=TextArea())
pub_date = DateTimeField(u'date create')
topic = QuerySelectField(query_factory=enabled_topics, allow_blank=True)
tags = TagListField(u'Tags') # here you use your custom field
# a method to set the tags
def fill_tags(self, tags):
self.tags.data = tags
# and from the view that is creating the form you send the list of tags
in your view: 在你看来:
form = PostForm()
form.fill_tags([tag.name for tag in post.tags.all()])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.