Model Versioning with Diff’s in Django v3_1
Created: March 20, 2021
Modified: March 22, 2021
What we start with is a simple model that has two fields; a title and a body. What we end up with is a collection of modifications done that allows us to see what changed in both fields for each modification performed. This is enabled by a plugin called django-simple-history and then leveraged with diff behind the scenes to give readability into what changed between the title and the body from version to version. In this article we will be leveraging the diff command in Ubuntu with Django version 3.1 via a command line utility that captures the output and then we use it to show in the view template after it has been cleaned up and made ready for HTML. At this point there is no caching and assumes your website will have very little traffic. More on that possibility in a later article.
Continuing with the document called Custom item URL’s in Django v3_1, get your app started
1. Start the Hyper-V in the Windows Search bar
2. Double click the name Ubuntu Server 20.4 LTS and it should launch a screen that is a terminal
3. Login if required, in other words if it is not already logged in
4. Change into the ~/OneDrive/code/example.com/git/project
5. Update to the latest version of Django
a. python3.8 -m pip install django==3.1.7
6. Run the server on the 8000 port
a. python3.8 manage.py runserver 0:8000
7. Load the site in your browser
a. http://192.168.0.12:8000/
Continue to build the poems app
1. Run the install command and get the django-simple-hijstory module installed
a. python3.8 -m pip install django-simple-history
2. Modify the project/project/settings_development.py and add to the INSTALLED_APPS
3. Add the HistoryRequestMiddleware to the MIDDLEWARE variable in the same document so we can automatically track changes done by each user within the history.
4. Add HistoricalRecords() call to the Poem model poems/models.py
5. Run the migrations to get the database ready
a. python3.8 manage.py makemigrations
b. python3.8 manage.py migrate
6. As found by referencing Kite [1] and Stack Overflow [2] and several other sources as mentioned at the end of the document to make this code concise; here’s how to get the diff of a change made by capturing the output of the command line from within your new view method in poems/views.py. This presumes your Poem will have multiple lines to compare with.
7. Add the following to poems/templatetags/poem_tag_extras.py as found on Stack Overflow [12]
8. Modify the poems/templates/poems/item.html to reflect all the changes of the item
9. Now run the populate_history command to get all the Poem data imported relative to the fixture we loaded in the previous article..
There you have it, a site that shows the differences between each version of change due to modifying either the title or the body of the Poem record. Thank you for reading this document. Please reach out to me by TXT message at 250-368-1324 and let me know if you had any questions and I will update where the article needs to be clarified.
Sources:
Note to the sources; Thank you very much! I really appreciate the contributions you each have made to the Python and Django community. If by chance I have used your research material in error, please contact me at the above-mentioned TXT number and I will refactor the article to coincide with any agreements I may have misunderstood. My apologies in advance.
[1] https://www.kite.com/python/answers/how-to-get-output-from-a-shell-command-in-python
[2] https://stackoverflow.com/questions/23929235/multi-line-string-with-extra-space-preserved-indentation
[3] https://getbootstrap.com/docs/4.0/utilities/text/
[4] https://stackoverflow.com/questions/3877623/in-python-can-you-have-variables-within-triple-quotes-if-so-how
[5] https://stackoverflow.com/questions/46781136/syntax-error-near-unexpected-token-when-using-multiple-input-streams-from-zc
[6] https://stackoverflow.com/questions/7011066/get-the-last-element-of-the-list-in-django
[7] https://stackoverflow.com/questions/11481499/django-iterate-number-in-for-loop-of-a-template
[8] https://docs.python.org/3/tutorial/controlflow.html
[9] https://stackoverflow.com/questions/23929235/multi-line-string-with-extra-space-preserved-indentation
[10] https://stackoverflow.com/questions/1107737/numeric-for-loop-in-django-templates
[11] https://docs.python.org/3/library/pprint.html
[12] https://stackoverflow.com/questions/13358063/template-tag-or-filter
[13] https://www.djangoproject.com/download/
[14] https://www.programiz.com/python-programming/methods/list/reverse












