Reputation: 1741
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)
Upvotes: 0
Views: 1606
Reputation: 1741
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():
Upvotes: 0
Reputation: 4793
This most likely will work in Flask, however I've only ever done this using Pyramid
db.session.is_modified(ptask)
#returns True/False
Upvotes: 2