如何将查询结果发送到 WTForm 字段? [英] How to send query results to a WTForm Field?
问题描述
我使用带有多对多表的 SQLalchemy 来管理博客文章标签.我需要帮助将标记值渲染到可以编辑它们的 TextArea 表单字段中.现在,当我呈现时,我看到了查找查询.
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.
模型
在`tags'中定义了Tag和Post的关系
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)
我的问题是在 WTF 中渲染标签字段.我在字段中显示查询而不是结果.
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.) 在视图中,通过标签进行交互并显示值.b.) 在自定义字段中,在渲染字段之前以某种方式传递帖子 ID 并运行查询.像这样的东西....
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 有效,但知道如何将帖子 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...
推荐答案
你用错了_value,它是用来显示数据的,不是用来设置数据的.
You are using _value incorrectly, it's used to display the data, not to set the data.
数据是在表单上设置的,而不是在字段上.
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
在您看来:
form = PostForm()
form.fill_tags([tag.name for tag in post.tags.all()])
这篇关于如何将查询结果发送到 WTForm 字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!