python - flask sqlalchemy signals 无法触发
怪我咯
怪我咯 2017-04-18 10:28:53
0
1
479

在我得蓝图当中我定义了 一个接受者

# -*- coding: utf-8 -*-

from flask_sqlalchemy import models_committed


# flag = True
# if flag:
def on_models_committed(sender, changes):
    print u'我是订阅者,我要触发任务'
    for obj, change in changes:
        if change == 'insert' and hasattr(obj, '__commit_insert__'):
            obj.__commit_insert__()
        elif change == 'update' and hasattr(obj, '__commit_update__'):
            obj.__commit_update__()
        elif change == 'delete' and hasattr(obj, '__commit_delete__'):
            obj.__commit_delete__()


models_committed.connect(on_models_committed)

然后哦在create_app 当中的__init__.py 导入了我蓝图中的这个方法

在models.py 中定义了__commit_update__()方法来执行处理逻辑

但是现在信号无法触发,求问哪里出错了?

怪我咯
怪我咯

走同样的路,发现不同的人生

reply all(1)
阿神
问题解决方式
db.session.query(ClassModel).filter(
            ClassModel.id == ids).delete()
修改成
db.session.query(ClassModel).filter(
            ClassModel.id == ids).first().delete()

classModel(db.Model):
    .....
    
    def delete(self):
        db.session.delete(self)
        db.session.commit()

With the above modifications, the signal trigger can be correctly captured, but I don’t know what the principle is? Is the deletion of the query object and the deletion of the model object a level issue? Are the built-in signals based on the db level?

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!