Undelete for Django

Django

01 April 2011

First off, this is not an April Fool’s joke. Stupid day, requiring a disclaimer. You know in France they just slap paper fish on the backs of their friends and laugh at those who don’t realize it’s there; sort of a single-form prank day rather than a practical joke day.

With that non-sequitor, I present django-undelete

This is a first stab at bringing Simon Willison’s post on the same topic into the Django 1.3 world. It is also very simple, and probably lacks a lot of the finesse needed for big projects. For example, I already know it only works when you’re using the delete() method, rather than catching pre_delete signals (version 2 perhaps?).

Still, it stands a fairly good chance of being useful to some people, and I’ve already found it nice to know that when a user deletes something via the admin, it’s not really gone.

This projects only feature right now is that you get a “TrashableMixin” that, if your model inherits from, gives you an invisible “trashed_at” datetime field and some managers to make sure you have access to trashed items and that you only see non-trashed items when you do querys.

Caveats: Because my django-extension-style-du-jour is currently model mixins, there are some issues if you have a whole bunch of mixins that are all competing for managers, be warned. Also, if you need to set your own manager on the “objects” hook, make sure you also re-inherit the NonTrashedManager, or else you’ll be seeind all your model objects, and not just the undeleted ones.

Discuss this post on Twitter with #onec-undelete

Creative 
Commons License