[Fixed]-Updating an auto_now DateTimeField in a parent model in Django

12👍

You would also need to then save the message. Then it that should work.

👤John

7👍

Proper version to work is: (attention to last line self.message.save())

class Message(models.Model):
    updated = models.DateTimeField(auto_now = True)
    ...

class Attachment(models.Model):
    updated = models.DateTimeField(auto_now = True)
    message = models.ForeignKey(Message)

    def save(self):
        super(Attachment, self).save()
        self.message.save()
👤Serjik

1👍

DateTime fields with auto_now are automatically updated upon calling save(), so you do not need to update them manually. Django will do this work for you.

👤zgoda

Leave a comment