How to send query results to a WTForm Field?

2019-01-29 07:52发布

问题:

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...

回答1:

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

in your view:

form = PostForm()
form.fill_tags([tag.name for tag in post.tags.all()])