Вопрос

After saving a value into my database, I am rendering an edit_field html.

the form auto-populates with prior data.

how do i save the original data so i can check which fields changed?

here is my skeleton edit view

@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)
Это было полезно?

Решение

This most likely will work in Flask, however I've only ever done this using Pyramid

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

Другие советы

As suggested by limasxgoesto0, I used the get_history, and it worked. here is me testing, and solving my how to test a boolean for change and assign a new date for fresh True's.

from my view:

@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():
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top