muya / muya.github.io

muya's blog
https://blog.muya.co.ke
MIT License
3 stars 2 forks source link

Implementing Timestamped Models in PeeWee #29

Open utterances-bot opened 2 years ago

utterances-bot commented 2 years ago

Implementing Timestamped Models in PeeWee – Muya's Blog

[mostly code] musings of a software developer from nairobi 🇰🇪 | learning

https://blog.muya.co.ke/timestamped-model-in-peewee/

muya commented 2 years ago

Comments Previously on Disqus

Originally by: coffin on 2016-02-25 15:01:00 Yeah, I get the same result if I use the second method. All the time is the same.Maybe this is because the python's class inheritance mechanism?

update:
I know why. The default value must be callable. This means you should use a function to wrap the current time.

Originally by: encore254 on 2016-02-25 20:33:46 Hey, could you give an example of that? I could add it to the post!

Originally by: coffin on 2016-03-10 15:48:40


def current_time():  
    return datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")  

class Example(BaseModel):  
    date_created = DateTimeField(default=current_time)   # not current_time()  

Originally by: gkmcd on 2016-06-08 09:19:52 I implemented something similar using Peewee's inbuilt signals decorators.

https://gist.github.com/gkm...

Originally by: travisjo on 2017-08-16 17:46:09 Does this work when Model.update is called? I've got a similar solution implemented but it's not picking up the current time when I expect it to. All updates are through Model.update(key=value).where(id=xx) methods.

Originally by: Matt on 2017-09-24 00:48:38 You need
Model.update(key=value).where(id==xx)
Note the difference between assignment (=) and comparison (==).