Last active
January 6, 2020 23:50
-
-
Save muya/37be3a87be65f64c4912 to your computer and use it in GitHub Desktop.
Timestamped Models in PeeWee
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
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
Sample code for implementing Timestamped Models in PeeWee. Accompanying blog post: https://blog.muya.co.ke/timestamped-model-in-peewee/