我正在尝试实现一种方法,该方法会将“其他”列的编辑日期添加到“编辑日期”列中。就像我连续更改用户名,并同时在“编辑日期”时出现了“ 23.08.18”类似。最好的方法是什么?
答案 0 :(得分:1)
首先,在模型中将pdfs = [x if '.pdf' in x else 'Not pdf' for x in list_of_files]
print(pdfs)
['test.pdf', 'Not pdf', 'yolo.pdf']
定义为DateTime列。
然后,您可以在模型视图类定义中覆盖editing_date
方法。只需将以下内容放入您的modelview类中
on_model_change()
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()
在创建或更新模型并将更改提交到数据库之前执行一些操作。每当将更改提交到数据库之前要运行额外的代码时,请使用此功能
答案 1 :(得分:1)
在模型中定义这样的列:
editing_date = db.Column(db.DateTime, onupdate=datetime.utcnow)