Reputation: 6203
I have the following two models:
class Blog(TimeStampedModel):
summary = models.TextField()
status = models.CharField(max_length=255)
class Entry(TimeStampedModel):
author = models.CharField(max_length=255)
text = models.TextField()
blog = models.ForeignKey(Blog, models.CASCADE, related_name='entries')
Both models sublass a common meta-model that defines a timestamp for when each model was last updated:
class TimeStampedModel(models.Model):
last_changed = models.DateTimeField(auto_now=True)
class Meta:
abstract = True
This works fine when saving each model individually. However, in my use case, when an Entry
is updated, it should also reflect in the update of the last_changed
timestamp of the associated Blog
.
Is there any easy way to tell Django to also bump the timestamps of related models?
Upvotes: 0
Views: 77
Reputation: 893
I admit that this is hacky, but you can override the save
method of Entry
model:
def save(self, *args, **kwargs):
self.blog.save()
super().save(*args, **kwargs)
Upvotes: 1