mergesort
mergesort

Reputation: 5187

How to update a field when another field is set to True using SQLAlchemy?

I have a visible field on my model, which is a boolean. When that model is updated I want to modify the published_at date field. If it is set to True, I want it to be the current time, if it is set to False, I want it to be None. How can I do that using SQLAlchemy?

Upvotes: 0

Views: 189

Answers (1)

Tasos Vogiatzoglou
Tasos Vogiatzoglou

Reputation: 2453

You can use sqlalchemy event listeners on the mapper you want. E.g.

@event.listens_for(MyObj, 'before_insert')
def update_ts_vector(mapper, connection, obj):
    if obj.updated
       obj.last_update = datetime.datetime.now()

Upvotes: 1

Related Questions