The Python Oracle

django - update date automatically after a value change

This video explains
django - update date automatically after a value change

--

Become part of the top 3% of the developers by applying to Toptal
https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Ancient Construction

--

Chapters
00:00 Question
00:33 Accepted answer (Score 13)
00:51 Answer 2 (Score 19)
01:50 Answer 3 (Score 11)
02:36 Answer 4 (Score 9)
02:59 Thank you

--

Full question
https://stackoverflow.com/questions/3185...

Answer 1 links:
[here]: https://docs.djangoproject.com/en/dev/re...

Answer 2 links:
[save_model]: https://docs.djangoproject.com/en/1.8/re...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #django #datetime #python3x

#avk47



ANSWER 1

Score 24


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)



ANSWER 2

Score 12


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.




ANSWER 3

Score 9


the following only changes your pub_date if published has been modified from False to True:

from datetime import datetime

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

def save(self, *args, **kwargs):
    if self.published and self.old_published != self.published:
        self.pub_date = datetime.now()
    super(Model, self).save(*args, **kwargs)



ANSWER 4

Score 2


Create a publish() method:

def publish(self):
    self.published = True
    self.pub_date = datetime.datetime.now()
    self.save()