Last active
April 7, 2023 07:05
-
-
Save TheBigRoomXXL/b7a753e18d4c5e86224c6597e082f455 to your computer and use it in GitHub Desktop.
Flask-SQLAlchemy patch method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask_sqlalchemy import SQLAlchemy | |
from flask_sqlalchemy.model import Model | |
class MyDbModel(Model): | |
"""MyDbModel is used as the "model_class" of db (aka db.Model). | |
Extend the SQLAlchemy functionalities throught this class.""" | |
def patch(self, update_dictionary: dict) -> None: | |
"""Partial update of an object with a simple and clear syntax""" | |
for col_name in self.__table__.columns.keys(): | |
if col_name in update_dictionary: | |
setattr(self, col_name, update_dictionary[col_name]) | |
db = SQLAlchemy(model_class=MyDbModel) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment