TimeStampedModel
- class specatalog.models.base.TimeStampedModel(**kwargs)[source]
Abstract base class providing automatic timestamp fields for all inheriting SQLAlchemy models.
This class adds two metadata fields,
created_atandupdated_at, which are populated automatically whenever an object is created or modified.- Parameters:
kwargs (Any)
- created_at
Timestamp indicating when the record was first created. Automatically assigned at insert time using
datetime.now(datetime.UTC).- Type:
datetime.datetime
- updated_at
Timestamp indicating when the record was last modified. Automatically updated before any SQL UPDATE operation. May be
Noneif the record has not been changed since creation.- Type:
datetime.datetime or None
Notes
The class is declared
__abstract__and therefore does not create its own database table.updated_atis updated using a SQLAlchemybefore_updateevent listener attached toTimeStampedModelwithpropagate=True.Model.queryis set toSession.query_property(), allowing models inheriting fromTimeStampedModelto useModel.queryin a Flask-style query interface (MyModel.query.filter(...)).
Examples
Creating a new model with timestamps:
>>> class Molecule(TimeStampedModel): ... __tablename__ = "molecules" ... id = Column(Integer, primary_key=True) ... name = Column(String)
>>> m = Molecule(name="Water") >>> session.add(m) >>> session.commit()
Accessing timestamps:
>>> m.created_at datetime.datetime(2025, 4, 12, 14, 30, tzinfo=datetime.UTC)
>>> m.updated_at None
After modification:
>>> m.name = "Heavy Water" >>> session.commit()
>>> m.updated_at datetime.datetime(2025, 4, 12, 14, 32, tzinfo=datetime.UTC)