Instantly share code, notes, and snippets.

Embed
What would you like to do?
Timestamped Models in PeeWee
from peewee import *
import datetime
database = SqliteDatabase("/data/amazing_people.db", **{})
class BaseModel(Model):
class Meta:
database = database
class TimestampedModel(BaseModel):
def save(self, *args, **kwargs):
if self._get_pk_value() is None:
# this is a create operation, set the date_created field
self.date_created = datetime.datetime.now().strftime(
"%Y-%m-%d %H:%M:%S")
self.date_updated = datetime.datetime.now().strftime(
"%Y-%m-%d %H:%M:%S")
return super(TimestampedModel, self).save(*args, **kwargs)
class Person(TimestampedModel):
name = TextField()
date_created = DateTimeField()
date_updated = DateTimeField()
class Meta:
db_table = 'person'
@muya

This comment has been minimized.

Copy link
Owner

muya commented Apr 15, 2015

Sample code for implementing Timestamped Models in PeeWee. Accompanying blog post: https://blog.muya.co.ke/timestamped-model-in-peewee/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment