How to control versions in django?

I have a Django webapp with various content (e.g. posts, topics, comments) and I want to track their history. Therefore, I am trying to create "version control" for "content". Here's how I do it (skip to the end of the question):

I have a Create_Content model that represents an action: it has an actor ( editor ), a set of modifications ( edition ), a timestamp, and the content it affects:

 class Create_Content(models.Model): editor = models.ForeignKey('User') edition = models.TextField() # pickled dictionary date = models.DateTimeField(auto_now_add=True) content = models.ForeignKey('Content') 

which is saved exactly once, because it is impossible to change the story.

I define the content:

 class Content(models.Model): last_edit = models.ForeignKey(Create_Content, related_name='content_last_id') first_edit = models.ForeignKey(Create_Content, related_name='content_first_id') def author(self): return self.first_edit.editor 

where last_edit is the last action performed, and first_edition I use to get the original author of the content.

All actual webapp content is implemented by derived Content models that are created / edited by Create_Content derived models. For instance,

 class Comment(Content): post = models.ForeignKey(Post, related_name="comment_set") body = models.TextField() 

It is created at the time of saving Create_Comment(Create_Content) (in fact, all subclasses of Create_Content are proxies).

For example, custom releases are instances of the Create_Content subclass (e.g. Edit_Comment(Create_Content) ), which, during save() does last_edit to indicate self and updates the actual contents (e.g. Edit_Comment.body ).

At the end, I am making a simplified version of Git for the database:

  • Create_Content are commits (which save the full state in self.edition ).
  • Content is a specific branch + working directory ( last_edit is a pointer),
  • The custom version moves the branch forward and changes the Content to new content (for example, body Comment ).

Brief completion

I realized that every user action will have an entry in the Create_Content table.

My question is:

how bad will this happen? I mean, this table can be quite large, and all actions will hit it.

I think my approach is โ€œclean,โ€ but I'm sure I am reinventing the wheel again. What is this particular wheel?

+4
source share
1 answer

You can try: https://bitbucket.org/emacsway/django-versioning . Django-versioning allows you to store data stored in django models, and saves only diff, not a copy of the content. Supports all field types except ManyToMany (currently).

Perhaps also useful for your project: the built-in Django comment application and a way to access objects. I think this is a good and clean approach.

The physical size of the database does not matter. The number of entries does not matter. See: How big is the MySQL database before performance starts to degrade

But how useful is it to keep the whole story? You can start deleting a story, for example TimeMachine. Daily (during the week), weekly (for several months) and monthly entries. Crontab or Django-Celery help delete an old story.

+2
source

Source: https://habr.com/ru/post/1493465/


All Articles