Skip to content

Instantly share code, notes, and snippets.

@TheBigRoomXXL
Last active April 7, 2023 07:05
Show Gist options
  • Save TheBigRoomXXL/b7a753e18d4c5e86224c6597e082f455 to your computer and use it in GitHub Desktop.
Save TheBigRoomXXL/b7a753e18d4c5e86224c6597e082f455 to your computer and use it in GitHub Desktop.
Flask-SQLAlchemy patch method
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