尝试使用Flask-SqlAlchemy编辑Sqlite条目,但收到错误

bfrts1fy  于 2023-04-30  发布在  SQLite
关注(0)|答案(1)|浏览(134)

当尝试编辑一行时,在提交时我得到这个错误:
sqlalchemy.exc.编程错误:(sqlite3.ProgrammingError)绑定参数1时出错:不支持类型“StringField”[SQL:UPDATE blog_post SET title=?,subtitle=?,body=?,author=?,img_url=?WHERE blog_post.id =?] [参数:(〈wtforms.fields.simple.StringField对象at 0x0000020F99680610〉,〈wtforms.fields.simple.StringField对象位于0x0000020F99680C10〉,〈flask_ckeditor.fields.CKEditorField object at 0x0000020F99682250〉,〈wtforms.fields.simple.StringField对象位于0x0000020F99682ED0〉,〈wtforms。fields.simple.StringField对象位于0x0000020F99682310〉,2)](此错误的背景位于:https://sqlalche.me/e/20/f405
我并没有试图插入另一种数据类型,所有的数据都是字符串。这是我的table:

class BlogPost(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(250), unique=True, nullable=False)
    subtitle = db.Column(db.String(250), nullable=False)
    date = db.Column(db.String(250), nullable=False)
    body = db.Column(db.Text, nullable=False)
    author = db.Column(db.String(250), nullable=False)
    img_url = db.Column(db.String(250), nullable=False)

下面是我编辑条目的代码:

@app.route("/edit/<int:post_id>", methods=["GET", "POST"])
def edit_post(post_id):
    with app.app_context():
        post = db.session.query(BlogPost).filter_by(id = post_id).first()
        print(post.__dict__)
        # post_data = CreatePostForm(obj=post)
        post_data = CreatePostForm(
                title = post.title,
                subtitle = post.subtitle,
                author = post.author,
                img_url = post.img_url,
                body = post.body
            )
        if post_data.validate_on_submit():
            print(post.title)
            post.title = post_data.title
            post.subtitle = post_data.subtitle
            post.author = post_data.author
            post.img_url = post_data.img_url
            post.body = post_data.body
            db.session.commit()

            return redirect(url_for('show_post', index = post_id))
        return render_template('make-post.html', form = post_data, is_edit = True)

我不知道这个程序错误是从哪里来的。

yqlxgs2m

yqlxgs2m1#

好的,我正在用Flask-WTF表单中的数据更新SqlAlchemy行。我忘了使用数据属性。所以post.title = post_data。title.数据
我的变量声明也令人困惑,post_data应该类似于edit_blog_form这样的post。title = edit_ www.example.com

@app.route("/edit/<int:post_id>", methods=["GET", "POST"])
def edit_post(post_id):
    with app.app_context():
        post = db.session.query(BlogPost).filter_by(id = post_id).first()
        print(post.__dict__)
        # post_data = CreatePostForm(obj=post)
        edit_blog_form= CreatePostForm(
                title = post.title,
                subtitle = post.subtitle,
                author = post.author,
                img_url = post.img_url,
                body = post.body
            )
        if post_data.validate_on_submit():
            post.title = edit_blog_form.title.data
            post.subtitle = edit_blog_form.subtitle.data
            post.author = edit_blog_form.author.data
            post.img_url = edit_blog_form.img_url.data
            post.body = edit_blog_form.body.data
            db.session.commit()

            return redirect(url_for('show_post', index = post_id))
        return render_template('make-post.html', form = post_data, is_edit = True)

相关问题