I have a model with a text field, that needs to be versioned.
class Book(models.Model):
title = models.CharField(max_length=100)
summary = models.TextField()
The expected behavior is as follows:
summary
should be versioned, not the whole modelHow should i do this?
Got it myself.
First create a new model called SummaryHistory
:
class SummaryHistory(models.Model):
version = models.IntegerField(editable=False)
book = models.ForeignKey('Book')
text = models.TextField()
class Meta:
unique_together = ('version', 'book',)
def save(self, *args, **kwargs):
# start with version 1 and increment it for each book
current_version = SummaryHistory.objects.filter(book=self.book).order_by('-version')[:1]
self.version = current_version[0].version + 1 if current_version else 1
super(SummaryHistory, self).save(*args, **kwargs)
Now extend the initial model as follows:
class Book(models.Model):
title = models.CharField(max_length=100)
summary = models.TextField()
def summary_history(self):
return SummaryHistory.objects.filter(book=self).order_by('-version')
def save(self, *args, **kwargs):
super(Book, self).save(*args, **kwargs)
# save summary history
summary_history = self.summary_history()
if not summary_history or self.summary != summary_history[0].text:
newSummary = SummaryHistory(book=self, text=self.summary)
newSummary.save()
Now, every time you update a book, a new incremented version of the summary for the specific book will be created unless it has not changed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With