I'm trying to automatize updates on database since I have a lot of columns. I have succeeded to display the database using for loop, but I have problem commiting changes to database:
@app.route('/listvehicle/<int:vehicle_id>/update', methods=['GET', 'POST'])
@login_required
def update_vehicle(vehicle_id):
vehicle = Vehicle.query.get_or_404(vehicle_id)
form = VehicleUpdateForm()
if form.validate_on_submit():
for attr,value in vehicle.__dict__.items():
for ia in form:
if ia == attr:
vehicle.__dict__[attr] = form.__dict__[ia]
db.session.commit()
flash(f'The user has been updated', 'success')
return redirect(url_for('listvehicle'))
elif request.method == 'GET':
for attr,value in vehicle.__dict__.items():
for ia,iv in form.__dict__.items():
if ia == attr:
iv.data = value
return render_template('updatevehicle.html', form=form)
Any help with commiting changes to database?
sqlalchemy? why are you not using aModelForm?