python - How does SQLAlchemy insert data into the corresponding id?
巴扎黑
巴扎黑 2017-05-24 11:34:51
0
2
716

I want to insert or modify a piece of data in the row corresponding to the id, but I don’t know how to do it

巴扎黑
巴扎黑

reply all(2)
習慣沉默

To add, delete, check and modify, just go to the document.

model = Model.query.get(id)
or model = Model.query.filter_by(id=id).first()

model.name = 'new name'

db.session.add(model)
db.session.commit()
黄舟

If you insert data by specifying the ID, it is unlikely if it is the primary key, but it can be updated. And if it is not the primary key, then 2 operations can be achieved. The code is similar to the following:

Add as:

model = Model(name='new names')
db.session.add(model)
db.session.commit()

Then update to:

model.name = 'new name'
db.session.add(model)
db.session.commit()
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!