更新基于 Flask 和 Peewee 的 Sqlite 数据库

Updating an Sqlite database, built on Flask and Peewee

我有一个 python 使用 Flask、PeeWee 和 Sqlite 构建的应用程序。我创建了一个在前端视图中显示的数据库 table。我试图做到这一点,以便用户可以单击 update/edit link,它将触发一个函数,该函数根据 table 行的 ID 显示表单。

然后用户可以使用表单更新 table 记录。

问题: 表格按预期显示,我可以添加信息并提交表格而不会出现错误,但是数据库中没有任何变化,即原始记录仍然与以前相同。

我尝试过的: 我试过使用 PeeWee 文档中的 save() 和 update() 函数,但我要么编码不正确,要么遗漏了一些东西。因此,带有表单的前视图将数据发送到一个名为 'change_standard' 的函数,该函数随后应该更新数据库,但没有。

相关代码如下。提前致谢:)

app.py

'''updates a standard'''
@app.route('/update_standard/<int:id>', methods=('GET', 'POST'))
@login_required
def update_standard(id):
    form = forms.UpdateForm()
    if form.validate_on_submit():
        models.Standards.change_standard(
            section=form.section.data,
            standard=form.standard.data)
        flash("Standard updated! Thanks!", "success")
        return redirect(url_for('view_standards'))
    return render_template('update-standard.html', form=form, id=id)

models.py

class Standards(Model):
    section = CharField(unique=False)
    standard = CharField(unique=True)

    class Meta:
        database = DATABASE


    @classmethod
    def create_standard(cls, section, standard):
        try:
            with DATABASE.transaction():
                cls.create(
                    section=section,
                    standard=standard
                )
        except IntegrityError:
            raise ValueError("Section or Standard already exists")


    @classmethod
    def delete_standard(cls, id, section, standard):
        try:
            with DATABASE.transaction():
                cls.delete(
                    id=id,
                    section=section,
                    standard=standard)
        except IntegrityError:
            raise ValueError("Standard does not exist for deletion")


    @classmethod
    def change_standard(cls, section, standard):
        try:
            with DATABASE.transaction():
                cls.update(
                    section=section,
                    standard=standard)
        except IntegrityError:
            raise ValueError("Standard does not exist for update")

forms.py

class UpdateForm(FlaskForm):
    section = TextAreaField('Enter new section', validators=[DataRequired(), section_exists])
    standard = TextAreaField('Enter new standard', validators=[DataRequired(), standard_exists])

您的 change_standard 方法应该如下所示才能生效(指定更新行的 ID 并执行查询):

    @classmethod
    def change_standard(cls, id, section, standard):
        try:
            with db.transaction():
                cls.update(
                    section=section,
                    standard=standard).where(cls.id==id).execute()
        except IntegrityError:
            raise ValueError("Standard does not exist for update")

参考link:Updating existing records

由于结合了@Tomasz Paluch 的回答和来自另一个站点的评论,所以这就是我所做的。我缺少的一点是以正确的方式引用记录 ID。

@classmethod
    def change_standard(cls, id, section, standard):
        try:
            with DATABASE.transaction():
                standards_to_update = cls.get_by_id(id) # specifies which Standards to update
                standards_to_update.section = section # updates the value for section
                standards_to_update.standard = standard # updates the value for standard
                standards_to_update.save() # saves changes
        except IntegrityError:
            raise ValueError("Standard does not exist for update")