Skip to content

Instantly share code, notes, and snippets.

@jacobwegner
Last active November 1, 2024 14:52
Show Gist options
  • Save jacobwegner/3c0794ba49c8332a3221ca5088b0f357 to your computer and use it in GitHub Desktop.
Save jacobwegner/3c0794ba49c8332a3221ca5088b0f357 to your computer and use it in GitHub Desktop.
Recovering data deleted via the Django admin

I was looking for a way to revert data deleted by mistake from the Django admin.

I reverse-engineered how Django's admin builds the list of objects displayed on the "Are you sure?" confirmation page.

Given a single obj or list of objs, the script included in this gist will:

  • Use the NestedObjects utility to determine the objects that would be deleted
  • Pass those objects to Django's JSON serializer
  • Write the objects to adeleted-objects fixture

This allowed me to:

  • Restore a database locally to a time before the object was deleted
  • Generate the fixture
  • Apply the fixture to the database to restore data
from django.core import serializers
from django.contrib.admin.utils import NestedObjects
from django.db import models, transaction, router
# assuming `obj` is the model instance for deletion:
objs = [obj]
using = router.db_for_write(obj._meta.model)
collector = NestedObjects(using=using)
collector.collect(objs)
def get_objects_callback():
for qs in collector.data.values():
for obj in qs:
yield obj
output_format = "json"
with open("deleted-objects.json", "w") as f:
serializers.serialize(output_format, get_objects_callback(), indent=2,
use_natural_foreign_keys=True,
use_natural_primary_keys=True,
stream=f)
@i-salameh95
Copy link

Glad that I could help you remember !
Thanks for replying, and clarifying that it works. I have the trust now to try it on live.

@bhaumikb
Copy link

I also tested this script and it is 100% working.
Thanks a lot, @jacobwegner for making this script.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment