Model mixins


class sqlalchemy_utils.models.Timestamp[source]

Adds created and updated columns to a derived declarative model.

The created column is handled through a default and the updated column is handled through a before_update event that propagates for all derived declarative models.

import sqlalchemy as sa
from sqlalchemy_utils import Timestamp

class SomeModel(Base, Timestamp):
    __tablename__ = 'somemodel'
    id = sa.Column(sa.Integer, primary_key=True)



Adds generic __repr__() method to a decalrative SQLAlchemy model.

In case if some fields are not loaded from a database, it doesn’t force their loading and instead repesents them as <not loaded>.

In addition, user can provide field names as arguments to the decorator to specify what fields should present in the string representation and in what order.


import sqlalchemy as sa
from sqlalchemy_utils import generic_repr

class MyModel(Base):
    __tablename__ = 'mymodel'
    id = sa.Column(sa.Integer, primary_key=True)
    name = sa.Column(sa.String)
    category = sa.Column(sa.String)

session.add(MyModel(name='Foo', category='Bar'))
foo = session.query(MyModel).options(sa.orm.defer('category')).one(s)

assert repr(foo) == 'MyModel(id=1, name='Foo', category=<not loaded>)'