Skip to main content

Abstraction to logical/soft delete in django models

Project description

Django Soft Delete

Build Status codecov

Django Soft Delete gives Django models the ability to soft delete(logical delete). it also gives the ability to restore or undelete soft-deleted instances.

Basic usage

  1. Clone this repo and then $pip install django-soft-delete
  2. Add softdelete model to INSTALLED_APPS
  3. Inherit all models you want to have this functionality from softdelete.models.SoftDeleteModel
>>> MyModel.objects.create(name='Anakin')
>>> MyModel.objects.create(name='Luke')
>>> MyModel.objects.create(name='Yoda')

>>> luke  = MyModel.objecs.filter(name='Luke')
>>> MyModel.objecs.filter(name='Luke').delete()

>>> MyModel.objects.count()
2

>>> MyModel.raw_objects.count()
3

>>> MyModel.objects.get(id=luke.id).undelete()
>>> MyModel.objects.count()
3

Samples

from softdelete.models import SoftDeleteModel


class MyModel(SoftDeleteModel):
    name = models.CharField(max_length=30)

You can also use the SoftDelete django manager to extends your custom manager funcionalities. Do it like so:

#my_model_manager.py
from softdelete.managers import SoftDeleteManager


class MyModelManager(SoftDeleteManager):

    def create_john_smith(self):
        self.model.objects.create(name='Jonh Smith')


#my_model.py
from django.db import models
from my_model_manager import MyModelManager


class MyModel(SoftDeleteModel):
    name = models.CharField(max_length=30)

    objects = models.Manager()
    my_manager = MyModelManager()

It's possible to have access to delete instances through an alternative manager raw_objects

    for inst in MyModel.raw_objects.all():
        print inst.name

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

soft-delete-0.2.2.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

soft_delete-0.2.2-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file soft-delete-0.2.2.tar.gz.

File metadata

  • Download URL: soft-delete-0.2.2.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.42.0 CPython/3.7.0

File hashes

Hashes for soft-delete-0.2.2.tar.gz
Algorithm Hash digest
SHA256 07ae2498e73972b17e73c2d1f6581c9908df08ec4cf2512e6cb1c4f6625babe4
MD5 d5b21a5a594b6319ccf42c5f74fcb12e
BLAKE2b-256 894e770cc7df017ba22787102feab965ce27425c52fcb386d9c42fcadfc7a03f

See more details on using hashes here.

File details

Details for the file soft_delete-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: soft_delete-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 4.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.42.0 CPython/3.7.0

File hashes

Hashes for soft_delete-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 83da0d442c6ad92759e43a9b074019e3fa150f92dc6fb520e9b6ba6a6b7e3c3e
MD5 605fbac1288777027698ec3aa0b1b7e8
BLAKE2b-256 473a54e90ae896e596aa07411021dee0ee1f4afcb995418127929a2e6f19e460

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page