Wednesday, August 20, 2014

SQLAlchemy's equivalent to Django's auto_now, auto_now_add

To define a datetime column table in SQLAlchemy with auto_now or auto_now_add default value which are used in Django models:


from sqlalchemy import Column, DateTime
import datetime

...
first_created = Column(DateTime(), default=datetime.datetime.now)

last_modified = Column(DateTime(), onupdate=datetime.datetime.now)
...