signals or triggers in SQLAlchemy

后端 未结 5 1683
被撕碎了的回忆
被撕碎了的回忆 2021-02-04 05:04

does SQLAlchemy have something similar to Django\'s signal concept? Basically, I\'d like to trigger a few functions when I pre-save or post-save some entity objects. Thanks.

5条回答
  •  情话喂你
    2021-02-04 05:42

    You can use inner MapperExtension class:

    class YourModel(db.Model):
    
        class BaseExtension(MapperExtension):
    
            def before_insert(self, mapper, connection, instance):
                # do something here
    
            def before_update(self, mapper, connection, instance):
                # do something here
    
        __mapper_args__ = { 'extension': BaseExtension() }
    
        # ....
    

提交回复
热议问题