How to update sqlalchemy orm object by a python dict

orm, python, sqlalchemy

Solution

You can use `setattr()` to update attributes on an existing SQLAlchemy object dynamically:

user = session.query(User).get(someid)

for key, value in yourdict.items():
    setattr(user, key, value)

Problem

the dict's key names are mapping to the sqlalchemy object attrs ex: ``` class User(Base): __tablename__ = 'users' id = Column(Integer, primary_key=True) name = Column(String) fullname = Column(String) password = Column(String) ``` can update from id = 3, `{name: "diana"}` or id = 15, `{name: "marchel", fullname: "richie marchel"}`

Original source