Skip to main content

Django Tracking Model / DTM 🏁

Track changes made to your model's instance fields. Changes are cleared on save. This package works well with signals. Mutable fields (e.g. JSONField) are not handled with deepcopy to keep it fast and simple. Meant to be model_utils's FieldTracker fast alternative.

Available on PyPi

Installation

pip install django-tracking-model

Usage

from django.db import models
from tracking_model import TrackingModelMixin

# order matters
class Example(TrackingModelMixin, models.Model):
    text = models.TextField(null=True)
    myself = models.ForeignKey("self", null=True)
    array = models.ArrayField(TextField())
In [1]: e = Example.objects.create(id=1, text="Sample Text")
In [2]: e.tracker.changed, e.tracker.newly_created
Out[1]: ({}, True)

In [3]: e.text = "Different Text"
In [4]: e.tracker.changed
Out[2]: {"text": "Sample Text"}

In [5]: e.save()
In [6]: e.tracker.changed, e.tracker.newly_created
Out[3]: ({}, False)

DTM will also detect changes made to ForeignKey/OneToOne fields.

In [1]: Example.objects.create(myself=e)
In [2]: e.myself = None
In [3]: e.tracker.changed
Out[1]: {"myself_id": 1}

Because DTM does not handle mutable fields well, you handle them with copy/deepcopy.

In [1]: e = Example.objects.create(array=["I", "am", "your"])
In [2]: copied = copy(e.array)
In [3]: copied.append("father")
In [4]: e.array = copied
In [5]: e.tracker.changed
Out[1]: {"array": ["I", "am", "your"]}

In [6]: e.array = ["Testing", "is", "the", "future"]  # in this case copy not needed

DTM works best with *_save signals.

def pre_save_example(instance, *args, **kwargs):
    # .create() does not populate .changed, we use newly_created
    if "text" in instance.tracker.changed or instance.tracker.newly_created:
      if instance.text
          instance.array = instance.text.split()

pre_save.connect(pre_save_example, sender=Example)
In [1]: e = Example.objects.create(text="I am your father")
In [2]: e.refresh_from_db() # not needed
In [3]: e.array
Out[1]: ["I", "am", "your", "father"]

DTM handles deferred fields well.

# from django.db.models.query_utils import DeferredAttribute
In [1]: e = Example.objects.only("array").first()
In [2]: e.text = "I am not your father"
In [3]: e.tracker.changed
Out[4]: {"text": DeferredAttribute}

You can narrow choice of tracked fields. By default everything is tracked.

class Example(models.Model):
    TRACKED_FIELDS = ["first"]
    first = models.TextField()
    second = models.TextField()

You can also implement your own Tracker class:

from tracking_model import Tracker

class SuperTracker(Tracker):
    def has_changed(self, field):
      return field in self.changed

class Example(TrackingModelMixin, models.Model):
    TRACKER_CLASS = SuperTracker

Requirements

  • Python >= 2.7, <= 3.13
  • Django >= 1.11, <= 5.X.Y

Metadata

Release files for django-tracking-model 0.1.8

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-tracking-model 0.1.8
File Size Uploaded
django_tracking_model-0.1.8.tar.gz 7.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-tracking-model 0.1.8
File Interpreter ABI Platform
django_tracking_model-0.1.8-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 11.6 kB

Release files / django_tracking_model-0.1.8.tar.gz

Download URL django_tracking_model-0.1.8.tar.gz
Size 7.0 kB
Tags Source
SHA-256 checksum
How to use checksums
acc2aac9b5336be1ca56edf614077c1b686fcb72834b89b4da1fb69ee8937c32
BLAKE2b-256 checksum
How to use checksums
ea6b5da23d0efaba4ce00bf9c3316d6677df21e2bf4174d51a438ef9988aff8a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.5

Release files / django_tracking_model-0.1.8-py2.py3-none-any.whl

Download URL django_tracking_model-0.1.8-py2.py3-none-any.whl
Size 4.6 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
8b5a2b66e7323004f424b81c2cbf2fb8319d18b0b915abdd681833c384cab932
BLAKE2b-256 checksum
How to use checksums
fff9fe545fce0f965d59be87a65755f1b8b35ef9ee1318fdccf7486f4ac27245
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.5

Release history Release notifications | RSS feed

This release

0.1.8 This release

2 release files

0.1.7

1 release file

0.1.6

2 release files

0.1.5

2 release files

0.1.4

1 release file

0.1.3

1 release file

0.1.2

1 release file

0.1.1

1 release file

0.1.0

2 release files

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