django - update date automatically after a value change

If you are setting the object as published in the Django admin, a nice way to do this is to override the save_model method of your model admin class.

from datetime import datetime

from django.contrib import admin


class MyModelAdmin(admin.ModelAdmin):
    def save_model(self, request, obj, form, change):
        if obj.published and 'published' in form.changed_data
            obj.pub_date = datetime.now()
            obj.save()

admin.site.register(MyModel, MyModelAdmin)

If you are setting the published flag elsewhere, then you can override the save() method, or use the pre_save signal. This isn't as elegant, because it's harder to tell whether the published flag has changed. I think you need to re-fetch the object from the database to check.


All answers were useful, but I finally did it this way:

def save(self, *args, **kwargs):
    if self.published and self.pub_date is None:
        self.pub_date = timezone.now()
    elif not self.published and self.pub_date is not None:
        self.pub_date = None
    super(Model, self).save(*args, **kwargs)

You want to add the auto_now field and set it to True. This will update with the current timestamp each time you update the model.

pub_date = models.DateTimeField('date_published', auto_now=True)

You can read about it here


Edit

Sorry you really just want to change the timestamp when the value of published is set to True. A really easy way to do this is to get the original value of the model and then override the save method so that it gets updated when it is set to True. Here is what you add to your code:

class MyModel(models.Model):
    published = models.BooleanField(default=False)
    pub_date = models.DateTimeField('date published')

    def __init__(self, *args, **kwargs):
        super(MyModel, self).__init__(*args, **kwargs)
        self._published = self.published

    def save(self, *args, **kwargs):
        if not self._published and self.published:
            self.pub_date = django.timezone.now()
        super(MyModel, self).save(*args, **kwargs)