如果你使用 MySQL,我相信你只能有一个自动更新的 datetime 列,所以我们使用 SQLAlchemy 的事件触发器来代替。
您只需将侦听器附加到“before_insert”和“before_update”挂钩并根据需要进行更新:
from sqlalchemy import event
@event.listen(YourModel, 'before_insert')
def update_created_modified_on_create_listener(mapper, connection, target):
""" Event listener that runs before a record is updated, and sets the create/modified field accordingly."""
target.created = datetime.utcnow()
target.modified = datetime.utcnow()
@event.listen(YourModel, 'before_update')
def update_modified_on_update_listener(mapper, connection, target):
""" Event listener that runs before a record is updated, and sets the modified field accordingly."""
# it's okay if this field doesn't exist - SQLAlchemy will silently ignore it.
target.modified = datetime.utcnow()
我知道没有人会记得将它添加到新模型中,所以我试图变得聪明并为他们添加它。
我们所有的模型都继承自一个我们巧妙地称为“DatabaseModel”的基础对象。我们检查谁继承了这个对象,并动态地将触发器添加到所有这些对象中。
如果模型没有 created 或 modified 字段也没关系 - SQLAlchemy 似乎默默地忽略它。
class DatabaseModel(db.Model):
__abstract__ = True
#...other stuff...
@classmethod
def _all_subclasses(cls):
""" Get all subclasses of cls, descending. So, if A is a subclass of B is a subclass of cls, this
will include A and B.
(Does not include cls) """
children = cls.__subclasses__()
result = []
while children:
next = children.pop()
subclasses = next.__subclasses__()
result.append(next)
for subclass in subclasses:
children.append(subclass)
return result
def update_created_modified_on_create_listener(mapper, connection, target):
""" Event listener that runs before a record is updated, and sets the create/modified field accordingly."""
# it's okay if one of these fields doesn't exist - SQLAlchemy will silently ignore it.
target.created = datetime.utcnow()
target.modified = datetime.utcnow()
def update_modified_on_update_listener(mapper, connection, target):
""" Event listener that runs before a record is updated, and sets the modified field accordingly."""
# it's okay if this field doesn't exist - SQLAlchemy will silently ignore it.
target.modified = datetime.utcnow()
for cls in DatabaseModel._all_subclasses():
event.listen(cls, 'before_insert', update_created_modified_on_create_listener)
event.listen(cls, 'before_update', update_modified_on_update_listener)