33

I am trying to add a creation_time attribute to my documents. The following would be an example:

import datetime

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

Django models have built in parameter for their DateTimeField objects like add_now, etc., but MongoEngine does not support this.

I am wondering if best way to do this is the following:

m,created = MyModel.objects.get_or_create()
if created:
    m.creation_date = datetime.datetime.now()

or if there is a better, nicer way.

Mike Mackintosh
  • 13,917
  • 6
  • 60
  • 87
josephmisiti
  • 9,862
  • 11
  • 56
  • 72

9 Answers9

75

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)
Willian
  • 2,385
  • 15
  • 17
  • This is exactly what I needed. I had figured out the default bit, but overriding the save method to track the modified time is perfect. Thank you :) – Dawson Feb 04 '14 at 18:21
  • 1
    The problem with this though is that the save function won't be called if you do an update instead of a .save right? – Nazariy Jul 10 '15 at 02:16
  • override `clean` method instead of `save` should be better, [docs here](http://docs.mongoengine.org/guide/document-instances.html) – xialu Aug 07 '18 at 07:02
  • 1
    The modified_date can't be updated when I use the function `update` – DachuanZhao Mar 31 '19 at 13:45
28

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.

Mike Mackintosh
  • 13,917
  • 6
  • 60
  • 87
Ben Hughes
  • 2,527
  • 1
  • 18
  • 16
  • 4
    This is better than the accepted answer. I've used `@property` on a function like this to get the created_time: `def created_at(self): return self.id.generation_time if self.id else None` – aaaronic Sep 12 '14 at 14:57
6

One nice solution is reusing a single signal handler for multiple documents.

class User(Document):
    # other fields...
    created_at = DateTimeField(required=True, default=datetime.utcnow)
    updated_at = DateTimeField(required=True)

class Post(Document):
    # other fields...
    created_at = DateTimeField(required=True, default=datetime.utcnow)
    updated_at = DateTimeField(required=True)

def update_timestamp(sender, document, **kwargs):
    document.updated_at = datetime.utcnow()

signals.pre_save.connect(update_timestamp, sender=User)
signals.pre_save.connect(update_timestamp, sender=Post)

Be careful to assign a callable and not a fixed-value as the default, for example default=datetime.utcnow without (). Some of the other answers on this page are incorrect and would cause created_at for new documents to always be set to the time your app was first loaded.

It's also always better to store UTC dates (datetime.utcnow instead of datetime.now) in your database.

codebutler
  • 171
  • 3
  • 3
4
# -*- coding: utf-8 -*-
from mongoengine import *
from mongoengine import signals
from datetime import datetime

class User(Document):
    email = StringField(required=True, unique=True)
    first_name = StringField(max_length=50)
    last_name = StringField(max_length=50)
    # audit fields
    created_on = DateTimeField(default=datetime.now())
    updated_on = DateTimeField(default=datetime.now())

    @classmethod
    def pre_save(cls, sender, document, **kwargs):
        document.updated_on = datetime.now()

signals.pre_save.connect(User.pre_save, sender=User)
allisson
  • 96
  • 1
4

If you are using the timestamp field in a bunch of Documents you can keep your code DRY by creating an abstract Document instead.

from datetime import datetime
from mongoengine import Document

class CreateUpdateDocument(Document):
    meta = {
        'abstract': True
    }

    # last updated timestamp
    updated_at = DateTimeField(default=datetime.now)

    # timestamp of when entry was created
    created_at = DateTimeField(default=datetime.now)

    def save(self, *args, **kwargs):
        if not self.created_at:
            self.created_at = datetime.now()
        self.updated_at = datetime.now()
        return super(CreateUpdateDocument, self).save(*args, **kwargs)
Vikas Gulati
  • 968
  • 2
  • 10
  • 24
  • 3
    You might want to put `self.updated_at = datetime.now()` under an `else` statement, so you can keep the idea of "if the updated_at is none, that means the model was never updated". – ldavid Jun 29 '15 at 13:19
4

My preferred solution is to use the @property decorator to return the creation datetime as extracted from the ObjectId:

@property
def creation_stamp(self):
    return self.id.generation_time
Emanuel Ey
  • 2,724
  • 5
  • 30
  • 38
1

Try use lambda value:

import datetime
from mongoengine import Document

class MyModel(Document):
    creation_date = mongo.DateTimeField()
    modified_date = mongo.DateTimeField(default=lambda : datetime.datetime.now())
JohnDHH
  • 409
  • 3
  • 7
0

Traditionally, I've set the creation_date default to datetime.now() and then have hidden the field on the admin form so you remove the possibility of a user overwriting the correct value. That requires almost no code.

Overriding the save method as suggested by Willian is also effective since you can programmtically block any updates to the creation_date and update the modfied_date at the same time.

Mike Mackintosh
  • 13,917
  • 6
  • 60
  • 87
-8

You could use auto_now_add parameter as per documentation:

class MyModel(mongoengine.Document):
    creation_date = mongo.DateTimeField(auto_now_add = True)
    modified_date = mongo.DateTimeField(auto_now = True)
Laur Ivan
  • 4,117
  • 3
  • 38
  • 62
  • 1
    That is most unfortunate. Then overriding the save as in the accepted answer is the right answer. I'd create a patch to add the feature though :) – Laur Ivan Nov 23 '11 at 10:28