I am learning how to use sqlalchemy and I am developing small app. I am having some issues trying to update data stored in the db using the ORM. I’m not sure what I am missing. The form edit_product.html gets populated with the right data however the return redirect(url_for statement is fired without updating the data. I am using merge(), I tried using add() as suggested by some tutorials, however if I do this I get an error saying that the record already exists.
This is my edit_product function in the views.py:
from database import db_session
@app.route('/product/edit/<int:product_id>', methods=['GET', 'POST'])
def edit_product(product_id):
product = Product.query.filter(Product.id == product_id).first()
form = NewOtrosForm(obj=product)
if request.method == 'POST':
print request.form
if form.validate():
form.populate_obj(product)
db_session.commit()
return redirect(url_for('product'))
else:
return render_template('edit_product.html', form=form)
This is what I have in my edit form in the jinja2 template:
{% from "_formhelpers.html" import render_field %}
<form method=post action="">
<dl>
{{ render_field(form.name) }}
{{ render_field(form.price) }}
{{ render_field(form.description) }}
{{ render_field(form.provider) }}
{{ render_field(form.detalles) }}
</dl>
<p><input type="submit" value="Save Changes"></p>
</form>
Can you try to change the below from :
to: