DEV Community

Mohsin Ashraf
Mohsin Ashraf

Posted on

Answer: How to get column and values dictionary in SQLAlchemy model?

You can use the following method which is inspired by the answer alecxe gave.

def get_model_dict(model, row)
    if isinstance(row, model):
        columns = [x.name for x in list(model.__table__.columns)]
        return {x: getattr(row, x) for x in columns}
    else:
        raise ValueError(f"The provided row is not of type {model.__table__.name.title()}")

Now all you have to…

Top comments (0)