使用sql alchemy更新多个字段

时间:2014-11-04 14:47:24

标签: python flask sqlalchemy flask-sqlalchemy

    user = { "email" : "my_new_email@example.com", "age" : 20, "city" : "London", "country" : "UK", "language" : "English", "profession" : "developer", "employeer" : "BBC" }

    admin = User.query.filter_by(username='admin').first()

    admin.email = user["email"]
    admin.age = user["age"]
    admin.city = user["city"]
    admin.country = user["country"]
    admin.language = user["language"]
    admin.profession = user["profession"]
    admin.employeer = user["employeer"]
    ....
    ....
    db.session.commit()

有什么方法可以避免这种隐式更新?我的意思是,如果更新只有一个或两个字段,这可以正常工作,但是没有实践大量更新和大量数据。

1 个答案:

答案 0 :(得分:1)

您可以将该字典提供给update子句:

data = { "email" : "my_new_email@example.com", "age" : 20, "city" : "London", "country" : "UK", "language" : "English", "profession" : "developer", "employeer" : "BBC" }
User.query.filter_by(username='admin').update(data)
db.session.commit()