Flask-SQLalchemy update a row's information

pocorschi picture pocorschi · Jul 14, 2011 · Viewed 163.5k times · Source

How can I update a row's information?

For example I'd like to alter the name column of the row that has the id 5.

Answer

Mark Hildreth picture Mark Hildreth · Jul 15, 2011

Retrieve an object using the tutorial shown in the Flask-SQLAlchemy documentation. Once you have the entity that you want to change, change the entity itself. Then, db.session.commit().

For example:

admin = User.query.filter_by(username='admin').first()
admin.email = '[email protected]'
db.session.commit()

user = User.query.get(5)
user.name = 'New Name'
db.session.commit()

Flask-SQLAlchemy is based on SQLAlchemy, so be sure to check out the SQLAlchemy Docs as well.