D
D
DEMonBond2020-03-21 17:28:54
Python
DEMonBond, 2020-03-21 17:28:54

Why is Flask-sqlalchemy's after_update event not being caught?

I'm trying to use a subscription to the after_update event in order to call a function when the field value in the database changes, when the ListenTask model changes, what am I doing wrong or don't understand?

When a new entry is added, the desired function is called.

class ListenTask(db.Model):
    __table_args__ = {'schema': SCHEMA}
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(256), index=True, unique=True)
    desc = db.Column(db.String(256), nullable=False)
    status = db.Column(db.Boolean, default=False)
    folder = db.Column(db.String(256), nullable=False)
    id_email = db.Column(db.Integer, db.ForeignKey('{}.email_settings.id'.format(SCHEMA)))
    id_owner = db.Column(db.Integer, db.ForeignKey('{}.user.id'.format(SCHEMA)))
    id_spreadsheets = db.Column(db.Integer, db.ForeignKey('{}.spreadsheets.id'.format(SCHEMA)))

    def __repr__(self):
        return '<Listen {}>'.format(self.id)


@event.listens_for(ListenTask, 'after_insert')
def event_after_insert(mapper, connection, target):
    # Здесь будет очень важная бизнес логика
    print('event_after_insert: ', current_user)
    print('target: ', target.id)


@event.listens_for(ListenTask, 'after_update')
def event_after_update(mapper, connection, target):
    # Здесь будет очень важная бизнес логика
    print('receive_after_update : ', current_user)
    print('target: ', target.id)


@app.route('/update_task_status/<id>', methods=['GET', 'POST'])
@login_required
def update_task_status(id):
    task_status = ListenTask.query.get(id).status
    ListenTask.query.filter_by(id=id).update({'status': (not task_status)})
    db.session.commit()
    return redirect(url_for('room'))

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question