Skip to main content

pypi actions codecov downloads clinicedc

django-revision

Add a Django field class to your models to track a revision number with every model instance saved.

python 3.12+, Django 5.2+. Uses GitPython.

Use django-revision in Django projects where you need to track the source code revision on each model instance on add and change.

  • If you deploy your live Django project from a cloned git branch, django-revision picks up the tag:branch:commit and updates each saved model instance as data is collected.

  • If you are not running your Django project from a cloned repository, django-revision can discover the revision number from other sources as described below.

When used together with django-simple-history, you can trace the revision number through the audit trail of a model instance.

Installation

Add to settings:

INSTALLED_APPS = [
    # ...
    'django_revision.apps.AppConfig',
    # ...
]

Add a revision field to your models

For example:

from django.db import models
from simple_history.models import HistoricalRecords
from django_revision import RevisionField

class TestModel(models.Model):
    revision = RevisionField()
    history = HistoricalRecord()

or use the RevisionModelMixin:

from django.db import models
from simple_history.models import HistoricalRecords
from django_revision.model_mixins import RevisionField

class TestModel(RevisionModelMixin, models.Model):
    history = HistoricalRecord()

then create or save a model instance

>>> test_model = TestModel.objects.create()
>>> test_model.revision
'0.1dev0'

If the source is modified after the git tag was applied:

>>> test_model = TestModel.objects.create()
>>> test_model.revision
'0.1dev0-35-ge9f632e:develop:e9f632e92143c53411290b576487f48c15156603'

How django-revision discovers the revision number

django-revision was originally developed to discover the git tag when a django project is run from a cloned git repository. By default, an exception will be raised if the working directory is not a git repository. However this behaviour can be bypassed by telling django-revision to ignore git discovery. django-revision will then try to discover a revision number from other sources. If all sources fail, an exception is raised.

The revision number is discovered in this order:

  1. from the git tag if working directory is a git repository

  2. from package metadata version()

  3. from [project][version] from pyproject.toml, if it exists

  4. from VERSION, if it exists

  5. from settings.REVISION

If the project using django-revision is not run from a git repo an exception will be raised by default. To bypass git discovery, update settings:

DJANGO_REVISION_IGNORE_WORKING_DIR = True

Discovery will now walk through the remaining options until one returns a value. To skip any one of the remaining options, update settings:

DJANGO_REVISION_IGNORE_METADATA = True

and / or

DJANGO_REVISION_IGNORE_TOML_FILE = True

and / or

DJANGO_REVISION_IGNORE_VERSION_FILE = True

You can hardcode a revision in settings as well (although this is not recommended):

REVISION = "1.0.0"

or

DJANGO_REVISION_REVISION = "1.0.0"

The settings.REVISION attribute is only used if the other options return None or you have told django-revision to ignore the other discovery options as shown above.

Relying on settings.REVISION

Hard coding settings.REVISION or settings. DJANGO_REVISION_REVISION is not recommended since you might forget to update the value and tag your data instances with the wrong revision number.

Using a git folder other than settings.BASE_DIR

By default, the git working directory is expected to be the same as settings.BASE_DIR. If not, add settings.GIT_DIR to settings with the path to your git working directory. For example:

GIT_DIR = Path(BASE_DIR).parent.parent

Using in a View and Template

In the view’s get_context_data set a context attribute to revision.tag or just use the RevisionMixin:

from django_revision.views import RevisionMixin

class MyView(RevisionMixin, TemplateView):
    pass

In your template:

{% block footer %}
  <footer class="footer">
    <div class="container">
      <div class="col-md-4"><p class="text-muted text-center"><small>{{ year }}&nbsp;{{ institution }}</small></p></div>
      <div class="col-md-4"><p class="text-muted text-center"><small>Revision: {{ revision }}</small></p></div>
      <div class="col-md-4"><p class="text-muted text-center"><small>For Research Purposes Only</small></p></div>
    </div>
  </footer>
{% endblock footer %}

Release files for django-revision 2.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-revision 2.1.1
File Size Uploaded
django_revision-2.1.1.tar.gz 15.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-revision 2.1.1
File Interpreter ABI Platform
django_revision-2.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 33.6 kB

Release files / django_revision-2.1.1.tar.gz

Download URL django_revision-2.1.1.tar.gz
Size 15.9 kB
Tags Source
SHA-256 checksum
How to use checksums
3ece3a98ba365e85bb18fa0a807504949c6a3bbc708421915333296062fc47db
BLAKE2b-256 checksum
How to use checksums
8ceef838d9516ae961d2aafbb2871e5118127fa4b7504bd18006af7ac76fb0f7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.0 {"installer":{"name":"uv","version":"0.12.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / django_revision-2.1.1-py3-none-any.whl

Download URL django_revision-2.1.1-py3-none-any.whl
Size 17.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
35172f872b35fd2db63c8d86445404cb42b88a8c7a07f6f6bcc2899bff305421
BLAKE2b-256 checksum
How to use checksums
562ae160b74c5cc5c486de0a34386ae6a0a757c197bdda94a0662da34beb35a2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.0 {"installer":{"name":"uv","version":"0.12.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release history Release notifications | RSS feed

This release

2.1.1 This release

2 release files

2.1.0

2 release files

2.0.3

2 release files

2.0.2

2 release files

2.0.1

2 release files

2.0.0

2 release files

1.0.0

2 release files

0.3.7

2 release files

0.3.6

2 release files

0.3.5

2 release files

0.3.4

2 release files

0.3.3

2 release files

0.3.1

1 release file

0.3.0

1 release file

0.1.20

1 release file

0.1.19

1 release file

0.1.18

1 release file

0.1.17

2 release files

0.1.15

2 release files

0.1.6

1 release file

0.1.5

1 release file

0.1.4

1 release file

0.1.3

1 release file

0.1.2

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page