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.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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_anonymise-0.5.0.tar.gz.
File metadata
- Download URL: django_anonymise-0.5.0.tar.gz
- Upload date:
- Size: 10.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c9137d9c23d1ca2aacab1d8e7d4ac561185825b41ec291f54c91b84f5ef65bd0
|
|
| MD5 |
9018538e0600e2ab7bc306ea46573513
|
|
| BLAKE2b-256 |
5553002e842c0b9955b6041c1f35f4343df3b1440e4f728e5a1f9f7fec8d478c
|
File details
Details for the file django_anonymise-0.5.0-py3-none-any.whl.
File metadata
- Download URL: django_anonymise-0.5.0-py3-none-any.whl
- Upload date:
- Size: 13.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d29eb422553126f303e68bb7f0b084dee6ba1741c76056a93d390dfeb008c366
|
|
| MD5 |
46b6a96010165dbafdb578b45b11abab
|
|
| BLAKE2b-256 |
ffd736442e769808c88600761a317e6f2e4f0269ff7bfa78b8737f2c4d04afad
|