Mongoengine creation_time attribute in Document

As an aside, the creation time is stamped into the _id attribute - if you do:

YourObject.id.generation_time

Will give you a datetime stamp.


You could override the save method.

class MyModel(mongoengine.Document):
    creation_date = mongo.DateTimeField()
    modified_date = mongo.DateTimeField(default=datetime.datetime.now)

    def save(self, *args, **kwargs):
        if not self.creation_date:
            self.creation_date = datetime.datetime.now()
        self.modified_date = datetime.datetime.now()
        return super(MyModel, self).save(*args, **kwargs)