Search code examples
mysqlpeewee

is there an auto update option for DateTimeField in peewee like TimeStamp in MySQL?


I would like a timestamp field updating each time the record is modified like in MySQL.

DateTimeField(default=datetime.datetime.now()) will only set it the first time it is created...

Any have a simple solution? Is the only solution is to manually set the Column options in MySQL db?


Solution

  • You can override the save method on your model class.

    class Something(Model):
        created = DateTimeField(default=datetime.datetime.now)
        modified = DateTimeField
    
        def save(self, *args, **kwargs):
            self.modified = datetime.datetime.now()
            return super(Something, self).save(*args, **kwargs)