Skip to content

Instantly share code, notes, and snippets.

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 =
"%Y-%m-%d %H:%M:%S")
self.date_updated =
"%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'

This comment has been minimized.

Copy link
Owner Author

@muya muya commented Apr 15, 2015

Sample code for implementing Timestamped Models in PeeWee. Accompanying blog post:


This comment has been minimized.

Copy link

@MrMahdi313 MrMahdi313 commented Jan 6, 2020

object has no _get_pk_value() attribute?
whats problem?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.