There are some collections that I created using flask-mongoEngine. For example, such as Author, Book, Company. The same part is they have 2 filed named
created_at = db.DateTimeField(required=True, default=utcnow())
updated_at = db.DateTimeField(required=True, default=utcnow())
Everytime I create a new document, I need to update the value of created_at and updated_at , also when I update the document, I need to update the value of updated_at. Are there a good way, when I do save() operation, these time field can auto update?
Setting the defaults to be utcnow() with the parentheses causes Python to execute the utcnow function when the class is created, not when a new object using that class is created. Instead, you should set the default to be the function without the (). That will pass the function as an object itself instead of calling it right away, and when a new object is created from this class, the function will execute.
Also, the Python docs recommend using datetime.now over utcnow. Going with that, your example should look like this:
import datetime
class Example(mongoengine.Document):
created_at = db.DateTimeField(required=True, default=datetime.datetime.now)
updated_at = db.DateTimeField(required=True, default=datetime.datetime.now)
As for updating the updated_at attribute whenever you call save(), you can create a wrapper function for save() that will set updated_at to datetime.now(), then save. Then, instead of calling save(), call your wrapper function whenever you want to save.
A wrapper function is a function that does a little preprocessing and then calls another function (see wikipedia: https://en.wikipedia.org/wiki/Wrapper_function).
For example, you write a function like this:
def my_save(object):
object.updated_at = datetime.datetime.now()
return object.save()
And then whenever you want to save an object, you call my_save() instead of object.save().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With