Skip to content

Instantly share code, notes, and snippets.

@danielholmstrom
Created April 29, 2014 12:20
Show Gist options
  • Save danielholmstrom/11398605 to your computer and use it in GitHub Desktop.
Save danielholmstrom/11398605 to your computer and use it in GitHub Desktop.
class CreatedAtMixin(object):
"""Adds a read-only column created_at to a table
An sqlalchemy.exc.ArgumentError will be raised it someone is trying to set
created_at. However, the '_created' column can still be set.
"""
_created = Column('created_at', DateTime,
server_default=text('NOW()'), nullable=False)
@hybrid_property
def created_at(self):
""" Get the column created """
return self._created
@created_at.setter
def set_created_at(self, created):
""" Prevents the column created from being set """
raise ArgumentError('Column created_at is read-only')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment