WTForm 不会在编辑模型时完全重新填充表单数据

WTForm does not entirely repopulate form data upon editing a model

Edit: Category field is now able to repopulate. Made a mistake in retrieving the data. App.py has been edited.

我最近创建了一个 "edit post" 表单,用户在其中单击 link,并且一些参数被发送到一个函数。我的函数搜索 post 并且 wtform 模块尝试使用目标 post 中的数据重新填充表单。 url (URLField), category (SelectField) 和 name ( StringField) 能够重新填充,但 details (TextField) 字段显示值 None。任何人都可以指导我在重新填充 flask-wtform 方面朝着正确的方向前进吗?

这是我的尝试:

app.py

@app.route('/edit_post/<int:post_id>', methods=("GET","POST"))
@login_required
def edit_my_post(post_id):
    posts = models.Post.select().where(models.Post.id == post_id)
    if posts.count() == 0:
        abort(404)


    if post_user.id == current_user.id :
        post = models.Post.select().where(models.Post.id == post_id).get()
        form = forms.ProductForm(obj=post)

        if form.validate_on_submit():
            form.populate_obj(post)
            query = models.Post.update(user = post.user.id,
                               name = form.name.data.strip(),
                               content = form.details.data.strip(),
                               url = form.url.data,
                               category = = form.category.data
                              ).where(models.Post.id == post_id)
            query.execute()
            flash("Your post has been edited.", "success")
            return redirect(url_for('index'))
    else:
        flash("Ay! Stay away from that post!","warning")
        return redirect(url_for('index'))

    return render_template("edit.html",form=form)

models.py

class Post(Model):
    timestamp = DateTimeField(default=datetime.datetime.now)
    user = ForeignKeyField(
        rel_model = User,
        related_name = 'posts'
    )
    name = TextField()
    content = TextField()
    upvotes = IntegerField(default=0)
    url = TextField()
    category = TextField()

    class Meta:
        database = DATABASE
        order_by = ('-timestamp',)

forms.py

class ProductForm(Form):
    name = StringField('Content Name', validators = [DataRequired()])
    url = URLField(validators=[url()])
    details = TextAreaField('Content Summary', validators = [DataRequired(),Length(max=140)])
    category = SelectField("Choose a category that your content fits in.", choices=CATEGORIES,coerce=int)

edit.html

{% extends "layout.html" %}
{% from 'macros.html' import render_field %}

{% block content %}
<form method="POST" action="">
  {{ form.hidden_tag() }}
  {% for field in form %}
    {{ render_field(field) }}
  {% endfor %}
  <br>
  <button id="submit" type="submit">Edit Post</button>
</form>
{% endblock %}

Note: CATEGORIES is a (long) array of strings.

我已通过简单地将详细信息 ProductForm 字段重命名为与 Post 表单上的 属性 相同的名称来解决此问题。所以现在表单代码如下所示:

class ProductForm(Form):
    name = StringField('Content Name', validators = [DataRequired()])
    url = URLField(validators=[url()])
    content = TextAreaField('Content Summary', validators = [DataRequired(),Length(max=140)])
    category = SelectField("Choose a category that your content fits in.", choices=CATEGORIES,coerce=int)

我重命名了部分查询以检索数据,如下所示:

query = models.Post.update(user = post.user.id,
                               name = form.name.data.strip(),
                               content = form.content.data.strip(),
                               url = form.url.data,
                               category = form.category.data
                              ).where(models.Post.id == post_id)
            query.execute()