MongoDB
 sql >> Base de Dados >  >> NoSQL >> MongoDB

Atributo create_time do Mongoengine no documento


Você pode substituir o método save.
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)