flask_sqlalchemy update
时间: 2023-10-01 16:07:54 浏览: 84
flask_sqlalchemy
To update a record in Flask SQLAlchemy, you can follow these steps:
1. Retrieve the record you want to update using a query.
```python
record = Model.query.filter_by(id=record_id).first()
```
2. Update the record fields as desired.
```python
record.field1 = new_value1
record.field2 = new_value2
```
3. Commit the changes to the database session.
```python
db.session.commit()
```
Here's an example of a function that updates a user's email address:
```python
from app import db, User
def update_user_email(user_id, new_email):
user = User.query.filter_by(id=user_id).first()
if user:
user.email = new_email
db.session.commit()
return True
else:
return False
```
阅读全文