mongoengine icon indicating copy to clipboard operation
mongoengine copied to clipboard

Updating date modified field on .update?

Open tony opened this issue 6 years ago • 6 comments

Related issue where this scenario was brought up: #21, #451

My response bringing it up again: https://github.com/MongoEngine/mongoengine/issues/451#issuecomment-414149971

To my knowledge (please correct me if I'm mistaken!) the signal patterns only work with .save(). In the project I'm working on, we use .update and QuerySet's .update_one in various locations. My intuition is that these would would trigger a signal?

(To my knowledge, we don't have update hooks in Django ORM either, in favor of auto_now/auto_now_add, per discussion in #21): https://code.djangoproject.com/ticket/21461

Assuming this doesn't happen - is there a canonical way to have a updated/updated_at/modified/modified_at field update with .update without overriding the method on the document?

Other things:

  • Examples in mongoengine documentation: http://docs.mongoengine.org/guide/signals.html#attaching-events (to my knowledge, this would only work if using .save()? So a codebase could potentially be missing modified changes?)
  • StackOverflow question: https://stackoverflow.com/q/8098122

tony avatar Aug 19 '18 19:08 tony

Maybe I'm wrong? Maybe this should be closed:

To be sure, here's the behavior of auto_now and auto_now_add on Django:

https://docs.djangoproject.com/en/2.1/ref/models/fields/#django.db.models.DateField.auto_now

The field is only automatically updated when calling Model.save(). The field isn’t updated when making updates to other fields in other ways such as QuerySet.update(), though you can specify a custom value for the field in an update like that.

That would mean that in Django, .update doesn't effect the modification time when using pre_save... That's a subletly that's news to me.

django_extensions.db.fields.ModificationDateTimeField

tony avatar Aug 19 '18 19:08 tony

Any word on this? I am in a similar situation as tony. I have tried overriding the save and clean methods and tried using signals to update a modified timestamp in my models. It also seems like using auto_now also does not work.

Thanks

lmzbonack avatar Apr 11 '20 21:04 lmzbonack

I'm also wondering when this functionality will be available in mongoengine. And if anyone could show me how to make a workaround to update "modified_at" field each time the .update() method called on a document it would be highly appreciated!

antonprokopovich avatar Nov 21 '20 14:11 antonprokopovich

@antonprokopovich did you figure it out?

bansalnaman15 avatar Feb 26 '21 00:02 bansalnaman15

To automatically update the "date modified" field whenever you call save(), consider these options:

1. Default Value: Set the default value of the field to datetime.datetime.now() in your model definition:

from datetime import datetime
from mongoengine import Document, DateTimeField

class YourModel(Document):
    # ... other fields
    date_modified = DateTimeField(default=datetime.datetime.now)

Use code with caution. Learn more This automatically sets the current time when a new document is created and updates it on any subsequent save() calls.

2. Pre-Save Hook:

Define a pre-save hook to modify the field before saving:

from mongoengine import signals

def set_date_modified(sender, document, **kwargs):
    document.date_modified = datetime.datetime.now()

signals.pre_save.connect(set_date_modified, sender=YourModel)

Use code with caution. Learn more This updates the field before every save() operation. Explicit Update within save():

3. Override the save() method in your model and update the field manually:

class YourModel(Document):
    # ... other fields
    def save(self, *args, **kwargs):
        self.date_modified = datetime.datetime.now()
        super().save(*args, **kwargs)

emamie avatar Feb 12 '24 11:02 emamie