GDPR on top of Django's model Collector

Original publication date: Sep. 30 2018
Django meetup presentation

In the rise of GDPR many organisations have started implementing more and more features to deal with "housekeeping" of user data, compliance with new data processing/retention policies and explicit requests for data export or removal.

In practice, it can be hard to figure out all the possible connections of related data when given a specific user entity. In the context of complex relational databases, we are probably faced with inspecting large trees of relations and constraints.

Django tries to partially solve this problem by defining and managing most relations and constraints on the application level. Users of the Django admin may be familiar with this delete confirmation view:

Django Admin user deletion view

This is possible because Django contains an object collector that will lookup those relations. When we run user.delete() Django will not just execute

DELETE FROM `myapp_users` WHERE `myapp_users`.`id` = x

but will first traverse all relations and collect all directly and indirectly related objects, which it will then clean up as appropriate. According to Django docs no ON DELETE CASCADE rule is ever defined in the DB, it is just emulated by the ORM.

While it can sometimes be frustrating to not have the DB fully manage those relations, we can, in some cases, use this Django feature to our advantage.

Collect data for export

Can we find out what data we have on this user?

This question pops up more and more frequently since the implementation of GDPR. The common use-case is when a user asks for an export of their data.

If we wanted to use the Django collector to get all related models for a given model, we could write something like:

from django.contrib.admin.utils import NestedObjects
def collect_user_data(user):
    collector = NestedObjects(using="replica")

This would return all data related to our user instance (as long as the ORM knows about the relations).

This can be built upon for more complex cases:

from django.contrib.admin.utils import NestedObjects

# Imaginary module, used as an example
from myapp.housekeeping import collect, IP_ASSOCIATED_MODELS 

class CustomUserCollector(NestedObjects):
    def __init__(self, user, *args, **kwargs):
        self.user = user
        if 'using' not in kwargs:
            kwargs['using'] = "replica"
        super(CustomUserCollector, self).__init__(*args, **kwargs)

    def collect_from_db(self):
        # Maybe we have some models that don't have a FK to user,
        # but are still somehow associated...
        for model in IP_ASSOCIATED_MODELS:
    def collect_from_nonrel(self):
        # Maybe we have other types of stores in our system
        # that may contain more user data...['couchdb'] = collect.from_couchdb(user)['redis_temp'] = collect.from_redis(user)

    def collect_remote(self):
        # Maybe there is an internal API where we store user data
        # with its own export mechanism['some_ext_service'] = collect.from_api(user)

    def collect_all():

def collect_user_data(user):
    collector = CustomUserCollector(user)

Report on deleted data

Can we keep track of how much data we're deleting?

So, we've been happily collecting and exporting data, but now it's time to delete some of it. Maybe we need to remove accounts after receiving deletion requests from users, or we may need to tidy up after data that's no longer used.

Deleted data is data we no longer need to really worry about (DISCLAIMER: I enjoy deleting stuff, including code).

If you simply remove an instance through the ORM, you will get back a summary of what was deleted. From the Django docs

# (5, {'webapp.Entry': 5})

Behind the scenes, django uses the same model collector to figure out what needs to be deleted and create a report.

A more complex deletion script could look something like:

def delete_all_the_things():
    inactive_users = fetch_inactive_users()
    users_pending_delete = fetch_pending_deletes()  
    for mailing_list in mailing_lists:
        unconfirmed_subs = fetch_unconfirmed_subs(mailing_list)

    # ...


If we wanted to check how many things we've just deleted, we would need to collect the output of each .delete() or log the individual statements.
We could also collect them and produce a summary, using an accumulator:

class DeleteStats(object):
    def __init__(self, delete_output=None):
        super(DeleteStats, self).__init__()
        self.total_removed = 0
        self.removed_by_model = defaultdict(int)

        if delete_output:

    def extend(self, delete_output):
        total_removed, removed_by_model = delete_output

        self.total_removed += total_removed
        for _model, _count in removed_by_model.iteritems():
            self.removed_by_model[_model] += _count

        return self

    def __getitem__(self, index):
        # Pretend to be a tuple to match how the django API returns deletes
        if index == 0:
            return self.total_removed
        elif index == 1:
            return self.removed_by_model
        raise IndexError()

also available as a Gist with mini-docs

import logging  
from myapp.housekeeping import DeleteStats

logger = logging.getLogger(__name__)

def delete_all_the_things(): 
    stats = DeleteStats()  
    inactive_users = fetch_inactive_users()
    users_pending_delete = fetch_pending_deletes()  
    for mailing_list in mailing_lists:
        unconfirmed_subs = fetch_unconfirmed_subs(mailing_list)
    # ...  
    total_deleted, removed_by_model = stats"Deleted {} objects in total".format(total_removed))

    for model_name, count in removed_by_model.iteritems():
"- {model_name} deleted: {count}".format(
                model_name=model_name, count=count))

IRL, instead of one long function, you could pass the accumulator into your cleanup functions, implement your own re-usable logging etc.


Finally, a one-off automated solution is not a replacement for solid process. New features will be added. New services will be deployed. Data will change form and move about. It is worth having a data-housekeeping checklist to reference in every change, to ensure that your automated processes stay up to date.