Skip to main content

Django Anonymiser

Django app for managing / tracking Django model anonymisation.

Status

This is currently used internally only, and has not been published to PyPI - use with caution.

Compatibility

  • Python: 3.12+
  • Django: 5.2, 6.0

Background

We currently have a pattern of each model having its own anonymise method, and a management command that iterates over each model calling said method on each object. This works, but it's impossible to track - we don't know which models, and which fields on those models, are actually being anonymised, and the documentation suffers the same fate as all documentation is that is not auto-generated.

This library adopts the pattern used by the django-side-effects library of having a "registry" of anonymisers and a management command that outputs the complete listing of all anonymisers and all fields anonymised. This output can then be plugged into the django-project-checks framework and stored in the repo as a "snapshot" that is then checked in the CI pipeline, meaning it is guaranteed to be up-to-date.

The anonymisation itself doesn't change - it's just shifting the code around.

Redaction vs. Anonymisation

This library contains two flavours of anonymisation - Redaction, and Anonymisation. The two differ in how the data is overwritten:

Type Implementation Performance Data
Redaction SQL Fast Table level
Anonymisation Python Slow Row level

Redaction

Redaction is implemented as a single SQL update statement that wipes an entire table in one go. It's very fast, but it's limited in the sense that it cannot produce realistic data. In fact it may well render your application unusable. It is recommended as the first step in data anonymisation.

Anonymisation

Anonymisation is an row-level operation that iterates over a queryset and updates each object in turn. The main advantage is that post-anonymisation you will have realistic, usable, data.

Usage

As an example - this is a hypothetical User model's anonymisation today:

# models.py
class User:

    def anonymise(self) -> None:
        self.first_name = "Fred"
        self.last_name = "Flinstone"

Using this library we remove the anonymise method and create and register a new anonymiser that splits out each field:

# anonymisers.py
@register_anonymiser
class UserAnonymiser(ModelAnonymiser):
    model = User

    def anonymise_first_name(self, obj: User) -> None:
        obj.first_name = "Fred"

    def anonymise_last_name(self, obj: User) -> None:
        obj.last_name = "Flintstone"

You should import the anonymisers module in your apps.py in order to ensure that it is registered:

# apps.py
from django.apps import AppConfig


class UsersConfig(AppConfig):
    name = "Users"

    def ready(self) -> None:
        super().ready()
        from . import anonymisers  # noqa F401

Once set up, running the display_model_anonymisation management command will output a list of all models in the project, whether they have a registered anonymiser, and then all model fields in the project and whether they are anonymised.

The snapshot for this project itself is tests/model_anonymisation.md.

The output format of the snapshot can be overridden - it's rendered using a Django template templates/display_model_anonymisation.md.

Metadata

Release files for django-anonymise 0.5.0

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-anonymise 0.5.0
File Size Uploaded
django_anonymise-0.5.0.tar.gz 10.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-anonymise 0.5.0
File Interpreter ABI Platform
django_anonymise-0.5.0-py3-none-any.whl Python 3 none any Details

Total release size: 24.6 kB

Release files / django_anonymise-0.5.0.tar.gz

Download URL django_anonymise-0.5.0.tar.gz
Size 10.8 kB
Tags Source
SHA-256 checksum
How to use checksums
c9137d9c23d1ca2aacab1d8e7d4ac561185825b41ec291f54c91b84f5ef65bd0
BLAKE2b-256 checksum
How to use checksums
5553002e842c0b9955b6041c1f35f4343df3b1440e4f728e5a1f9f7fec8d478c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.2

Release files / django_anonymise-0.5.0-py3-none-any.whl

Download URL django_anonymise-0.5.0-py3-none-any.whl
Size 13.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d29eb422553126f303e68bb7f0b084dee6ba1741c76056a93d390dfeb008c366
BLAKE2b-256 checksum
How to use checksums
ffd736442e769808c88600761a317e6f2e4f0269ff7bfa78b8737f2c4d04afad
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.2

Release history Release notifications | RSS feed

This release

0.5.0 This release

2 release files

0.4.0

2 release files

0.3.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