Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 834
Xelt Avatar asked Aug 29 '13 23:08

Xelt


People also ask

Does mysql update automatically?

An auto-updated column is automatically updated to the current timestamp when the value of any other column in the row is changed from its current value. An auto-updated column remains unchanged if all other columns are set to their current values.

What does a timestamp do on update Current_timestamp data type?

With the DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP, a column has the current timestamp for its default value and is automatically updated to the current timestamp.


1 Answers

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) 
like image 70
coleifer Avatar answered Oct 28 '22 21:10

coleifer