[flask_sqlalchemy ]插入数据时发生错误后如何处理

时间:2022-12-10 16:29:07

示例代码:

def add_supplier():
form = request.form
if request.method == 'POST':
print(form)
supplier = Supplier(form['supplier_name'],
form['supplier_constract'],
form['supplier_phone'],
form['supplier_address'],
form['supplier_remark'])
try:
db.session.add(supplier)
db.session.commit()
flash('供应商添加成功!', 'success')
except:
db.session.rollback()
flash('输入不合法,请重新输入!', 'danger')
return redirect(url_for('supplier.list_supplier'))

参考文档:

flask_sqlalchemy 插入数据时发生错误后如何处理?