sshkrv
sshkrv

Reputation: 131

Save flask-admin editing history

I'm trying implement method that will add to column 'editing-date' the date of editing OF ANOTHER column. Like I'm changing username in a row and simultaneously on 'editing-date' appears something like '23.08.18'. What is the best way to do it?

Upvotes: 0

Views: 474

Answers (2)

gittert
gittert

Reputation: 1308

In your model define a column like this:

    editing_date = db.Column(db.DateTime, onupdate=datetime.utcnow)

Upvotes: 1

shifloni
shifloni

Reputation: 719

First, define editing_date in your model as DateTime column.

then you can override the on_model_change() method in your modelview class definition. Just place the below in your modelview class

from datetime import datetime
on_model_change(form, model, is_created):
    if not is_created: # True if model was created and to False if edited
                       # you only want this to run when model is edited so we are using not operator
        model.edit_date = datetime.utcnow()

on_nodel_view() performs some actions before a model is created or updated and changes are committed to the database. Use this whenever you want to run extra code before the changes are committed to the database

Upvotes: 1

Related Questions