Giter Site home page Giter Site logo

django-softdelete's Introduction

What is different in this fork

Normally each record would have a 'deleted' checkbox next to it that would allow any staff member to (un)delete an object quickly and easily by (un)checking the box.

A side effect of this is that all objects, soft-deleted or otherwise are shown in the admin by default.

For my uses I would rather not show (soft) deleted objects, so in this fork they are not shown. The way to undelete soft-deleted records is therefore handled via the changesets.

This pattern allows me (the admin) to quickly undo any erroneous deletes made by staff.

django-softdelete

Soft delete for Django ORM, with support for undelete.

Limitations

  • No facility (at present) to provide undelete for 3rd-party models such as django.contrib.auth.models.User. If this is required then django-reversion might be a better fit.

Gotchas

  • If you override a model's default manager, ensure that you inherit from softdelete.models.SoftDeleteManager.

Can be tested directly with the following command:

django-admin.py test softdelete --settings="softdelete.settings"

Inspired by http://codespatter.com/2009/07/01/django-model-manager-soft-delete-how-to-customize-admin/

This project mostly exists to be able to provide undelete of soft-deleted objects, along with proper undeletion of related objects.

Requirements:

  • Django 1.3
  • django.contrib.contenttypes

Installation

To install this particular fork you can run:

pip install "git+git://github.com/shaflidason/django-softdelete.git#egg=django-softdelete"
  1. Add 'softdelete' to your INSTALLED_APPS.
  2. Have your models extend from softdelete.models.SoftDeleteObject. If you have existing models, use Django South to create a schema migration to add the deleted_at Datetime field to each of your models.
  3. Extend from or use directly the softdelete.admin.SoftDeleteObjectAdmin model admin class in your admin.py files.

There are simple templates files in templates/. You will need to add Django's egg loader to use the templates as is:

TEMPLATE_LOADERS = (
...
    'django.template.loaders.eggs.Loader',
)

Add the project 'softdelete' to your INSTALLED_APPS for through-the-web undelete support.

INSTALLED_APPS = (
...
    'django.contrib.contenttypes',
    'softdelete',
)

Central to the ability to undelete a soft-deleted model is the concept of changesets. When you soft-delete an object, any objects referencing it via a ForeignKey, ManyToManyField, or OneToOneField will also be soft-deleted. This mimics the traditional CASCADE behavior of an SQL DELETE.

When the soft-delete is performed, the system makes a ChangeSet object which tracks all affected objects of this delete request. Later, when an undelete is requested, this ChangeSet is referenced to do a cascading undelete.

If you are undeleting an object that was part of a ChangeSet, that entire ChangeSet is undeleted.

Once undeleted, the ChangeSet object is removed from the underlying database with a regular ("hard") delete.

django-softdelete's People

Contributors

scoursen avatar styrmis avatar dgaus avatar relekang avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.