Skip to content

Instantly share code, notes, and snippets.

@muya
Last active January 6, 2020 23:50
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save muya/37be3a87be65f64c4912 to your computer and use it in GitHub Desktop.
Save muya/37be3a87be65f64c4912 to your computer and use it in GitHub Desktop.
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
Copy link
Author

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/

@mahdizojaji
Copy link

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