some_json
{
"id": 1,
"foo": "foo",
"bar": "bar",
"baz": "baz"
}
assume all the keys in some_json are columns
then normally when i update the database, i'll do something like
item = db.query.filter_by(id=some_json['id']).first()
if item:
item.foo = some_json['foo']
item.bar = some_json['bar']
item.baz = some_json['baz']
db.session.add(item)
db.commit()
however, this can obviously be tedious if have more and more keys is there an easier way to update the table rows just by simply passing the json?
isinstance(some_json, dict)isTruethen you could try passingsome_jsonas parameter values to aninsert()statement, e.g.,db.session.execute(item.__table__.insert(), some_json)instance = MyModel(**json_dict); db.add(instance)ifjson_dictis a flatdictand its keys match column names inMyModel.