flask使用wtf表单编辑数据库字段并保留以前的值

2024-06-26 14:27:43 发布

您现在位置:Python中文网/ 问答频道 /正文

在将一个值保存到数据库中之后,我将呈现一个编辑字段html。在

表单将自动填充先前的数据。在

如何保存原始数据以便检查哪些字段已更改?在

这是我的骨架编辑视图

@app.route('/edit/<name>/<goal>/<strategy>/<task>', methods=['GET', 'POST'])
def edit_task(name,goal,strategy,task):
    ptask=models.Tasks.query.filter_by(task=task).first()
    form = task_form(obj=ptask)
    form.populate_obj(ptask)
    tform=task_form(request.values)
    if request.method == 'POST' and form.validate_on_submit():
        complete=tform.complete.data

        #check if complete changed 

        db.session.commit()
        return redirect(url_for('task_outline',name=name,goal=goal,strategy=strategy))
    return render_template('edit_task.html', tform=tform,form=form,ptask=ptask)

Tags: nameformobj编辑taskifrequesthtml
2条回答

这很可能会在烧瓶中工作,但我只使用过金字塔

db.session.is_modified(ptask)
#returns True/False

正如limasxgoesto0所建议的,我使用了get_历史,它起作用了。这是我的测试,并解决如何测试布尔值的变化和分配新的日期为新的真

在我看来:

@app.route('/edit/<name>/<goal>/<strategy>/<task>', methods=['GET', 'POST'])
def edit_task(name,goal,strategy,task):
   ..... more view missing here.....
   ptask=models.Tasks.query.filter_by(task=task)
   if request.method == 'POST' and form.validate_on_submit(): 
        print 'now ',get_history(ptask, 'complete')[0]
        print 'before ',get_history(ptask, 'complete')[2]
        if get_history(ptask, 'complete')[0]==[True] and get_history(ptask, 'complete')[2]==[False]:
            print 'changed from false to true'
            ptask.completeDate=datetime.datetime.utcnow()
        if get_history(ptask, 'complete')[0]==[False] and get_history(ptask, 'complete')[2]==[True]:
            print 'changed from true to false'
            ptask.completeDate=None
    db.session.commit()    if request.method == 'POST' and form.validate_on_submit():

相关问题 更多 >