Skip to main content

Django app used to manage production data anonymisation.

Project description

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.11+
  • Django: 4.2, 5.0, 5.2

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_anonymise-0.4.0.tar.gz (10.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_anonymise-0.4.0-py3-none-any.whl (13.7 kB view details)

Uploaded Python 3

File details

Details for the file django_anonymise-0.4.0.tar.gz.

File metadata

  • Download URL: django_anonymise-0.4.0.tar.gz
  • Upload date:
  • Size: 10.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.13.3 Darwin/24.5.0

File hashes

Hashes for django_anonymise-0.4.0.tar.gz
Algorithm Hash digest
SHA256 2313d2f463a71ff2586634b6f01801f6a2efc09a62a17e1229f877d9180e5052
MD5 c799bf5ce24703fe355a400982f4ce01
BLAKE2b-256 af3a5f95beaca637ae0e9c29cfde4d15c621417ec4a1ebe9098dd8b4c50a2c09

See more details on using hashes here.

File details

Details for the file django_anonymise-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: django_anonymise-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 13.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.13.3 Darwin/24.5.0

File hashes

Hashes for django_anonymise-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3aa3e38b00ab5928abcefe0cc147165b4441c117d8eb0490ace1f38a70feecc0
MD5 5adedc0e7809a92905a31aaa7a2905e2
BLAKE2b-256 e35e7a555a2d1aba80264f586ec73ec5a5885554250060e27ab3f0cf988a35d6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page