Skip to main content

django-model-changes allows you to track the state and changes of a model instance.

Requirements: Python 3.14+, Django 5.0-6.x

Quick start

  1. Install django-model-changes:

    uv add django-model-changes

    Or with pip:

    pip install django-model-changes
  2. Add the ChangesMixin to your model:

    >>> from django.db import models
    >>> from django_model_changes import ChangesMixin
    
    >>> class User(ChangesMixin, models.Model):
    >>>     name = models.CharField(max_length=100)
  3. Get instance changes:

    >>> user = User()
    >>> user.name = 'Foo Bar'
    >>> user.save()
    
    >>> user.name = 'I got a new name'
    
    >>> # Get current state
    >>> user.current_state()
    {'id': 1, 'name': 'I got a new name'}
    
    >>> # Get previous state (state after previous save/create/delete)
    >>> user.previous_state()
    {'id': 1, 'name': 'Foo Bar'}
    
    >>> # Get old state (state before previous save/create/delete)
    >>> user.old_state()
    {'id': None, 'name': ''}
    
    >>> # Get changes from the previous state to the current state
    >>> user.changes()
    {'name': ('Foo Bar', 'I got a new name')}
    
    >>> # Get changes from the old state to the current state
    >>> user.old_changes()
    {'id': (None, 1), 'name': ('', 'Foo Bar')}
    
    >>> # Check if the instance was persisted (saved)
    >>> user.was_persisted()
    False
    
    >>> # Check if the instance is persisted
    >>> user.is_persisted()
    True
  4. Listen for changes:

    >>> from django_model_changes import post_change
    >>> def my_callback(sender, instance, **kwargs):
    >>>     # Do something with previous and current state
    >>>     instance.old_state()
    >>>     instance.current_state()
    
    >>>     # There is also a convenience method to get
    >>>     # an instance from the previous state
    >>>     instance.old_instance()
    
    >>> post_change.connect(my_callback, User)
    

Overview

django-model-changes allows you to retrieve the following states from an instance:

  1. current_state()

    The current state of the instance.

  2. previous_state()

    The state of the instance after it was created, saved or deleted the last time.

  3. old_state()

    The previous previous_state(), i.e. the state of the instance before it was created, saved or deleted the last time.

It also provides convenience methods to get changes between states:

  1. changes()

    Changes from previous_state to current_state.

  2. previous_changes()

    Changes from old_state to previous_state.

  3. old_changes()

    Changes from old_state to current_state.

And the following methods to determine if an instance was/is persisted in the database:

  1. was_persisted()

    Was the instance persisted in its old state.

  2. is_persisted()

    Is the instance is_persisted in its current state.

This schematic tries to illustrate how these methods relate to each other:

after create/save/delete            after save/delete                  now
|                                   |                                  |
.-----------------------------------.----------------------------------.
|\                                  |\                                 |\
| \                                 | \                                | \
|  old_state()                      |  previous_state()                |  current_state()
|                                   |                                  |
|-----------------------------------|----------------------------------|
|  previous_changes() (prev - old)  |  changes() (cur - prev)          |
|-----------------------------------|----------------------------------|
|                      old_changes()  (cur - old)                      |
.----------------------------------------------------------------------.
 \                                                                      \
  \                                                                      \
   was_persisted()                                                        is_persisted()

Documentation

Refer to the doc strings in changes.py, or build the documentation:

>>> pip install Sphinx
>>> cd docs
>>> make html
Open build/html/index.html

Release files for django-model-changes 1.0.3

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-model-changes 1.0.3
File Size Uploaded
django_model_changes-1.0.3.tar.gz 10.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-model-changes 1.0.3
File Interpreter ABI Platform
django_model_changes-1.0.3-py3-none-any.whl Python 3 none any Details

Total release size: 17.4 kB

Release files / django_model_changes-1.0.3.tar.gz

Download URL django_model_changes-1.0.3.tar.gz
Size 10.9 kB
Tags Source
SHA-256 checksum
How to use checksums
c966572cb168095e85e1516be2dff77f65d8f12ba0aeb51e0b2f4b6c25caf7c1
BLAKE2b-256 checksum
How to use checksums
249587c5ab09278156ec7339295620c0bd759600922ea698f198332f442aaeec
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 23, 2025.

Transparency log

Release files / django_model_changes-1.0.3-py3-none-any.whl

Download URL django_model_changes-1.0.3-py3-none-any.whl
Size 6.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
39855fd865fecc636d221bd2b7e50b346610119925d183e0c3eef4c96e111054
BLAKE2b-256 checksum
How to use checksums
b953df76a9ccfa208b0874429cd9bda7261ed5447d9ecf259e2ad1792caf1fe4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 23, 2025.

Transparency log

Release history Release notifications | RSS feed

This release

1.0.3 This release

2 release files

1.0.2

2 release files

1.0.1

2 release files

0.15

1 release file

0.14

1 release file

0.13

1 release file

0.12

1 release file

0.11

1 release file

0.1

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