Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7917937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T15:19:44+00:00 2026-06-03T15:19:44+00:00

I have a model with a text field, that needs to be versioned. class

  • 0

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:

  • If i create a new book with a summary, the text will be saved normally
  • If the summary is updated, the old state needs to be stored somewhere with a version number and timestamp
  • It should be easily possible to query for the current, a range of or specific versions
  • Only the field summary should be versioned, not the whole model

How should i do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T15:19:48+00:00Added an answer on June 3, 2026 at 3:19 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have model Article it has field title with some text that may contain
I have two models: class Studio(models.Model): name = models.CharField(Studio, max_length=30, unique=True) class Film(models.Model): studio
I have a ModelForm field that is based on the following Model: class Phrase(models.Model):
I have two models: class Customer(models.Model): (...) class CustomerMemo(models.Model): (...) customer = models.ForeignKey(Customer) text
I have a text field in my model that contains markdown text. I need
I have two models like this: class OptionsAndFeatures(models.Model): options = models.TextField(blank=True, null=True) entertainment =
I have following models setup in my Django application class School(models.Model): name = models.TextField()
I have a Project model and it has some text attributes, one is summary.
I have the following code in a view: <div class=wrap_select> @Html.DropDownList(dateRange, new SelectList(Model.DateRange, Value,Text),
I have a model and in that model I'm generating a more complex field

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.