This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (pypi.python.org).
Help us improve Python packaging - Donate today!

Yet another approach to provide soft (logical) delete or masking (thrashing) django models instead of deleting them physically from db.

Project Description

Django Permanent

Yet another approach to provide soft (logical) delete or masking (thrashing) django models instead of deleting them physically from db.

Models

To create a non-deletable model just inherit it from PermanentModel:

class MyModel(PermanentModel):
    pass

It automatically changes delete behaviour to hide objects instead of deleting them:

>>> a = MyModel.objects.create(pk=1)
>>> b = MyModel.objects.create(pk=2)
>>> MyModel.objects.count()
2
>>> a.delete()
>>> MyModel.objects.count()
1

To recover a deleted object just call its restore method:

>>> a.restore()
>>> MyModel.objects.count()
2

Use the force kwarg to enforce physical deletion:

>>> a.delete(force=True) # Will act as the default django delete
>>> MyModel._base_manager.count()
0

If you need to restore a deleted object instead of re-creating the same one use the restore_on_create attribute:

class MyModel(PermanentModel):
    class Permanent:
      restore_on_create = True

In this case QuerySet provides check existence of same attribute objects and restores them if they’ve been deleted, creating new ones if not.

Managers

It changes the default model manager to ignore deleted objects, adding a deleted_objects manager to see them instead:

>>> MyModel.objects.count()
2
>>> a.delete()
>>> MyModel.objects.count()
1
>>> MyModel.deleted_objects.count()
1
>>> MyModel.all_objects.count()
2
>>> MyModel._base_manager.count()
2

QuerySet

The QuerySet.delete method will act as the default django delete, with one exception - objects of models subclassing PermanentModel will be marked as deleted; the rest will be deleted physically:

>>> MyModel.objects.all().delete()

You can still force django query set physical deletion:

>>> MyModel.objects.all().delete(force=True)

Using custom querysets

  1. Inherit your query set from PermanentQuerySet:

    class ServerFileQuerySet(PermanentQuerySet)
        pass
    
  2. Wrap PermanentQuerySet or DeletedQuerySet in you model manager declaration:

    class MyModel(PermanentModel)
        objects = MultiPassThroughManager(ServerFileQuerySet, NonDeletedQuerySet)
        deleted_objects = MultiPassThroughManager(ServerFileQuerySet, DeletedQuerySet)
        all_objects = MultiPassThroughManager(ServerFileQuerySet, PermanentQuerySet)
    

Method get_restore_or_create

  1. Check for existence of the object.
  2. Restore it if it was deleted.
  3. Create a new one, if it was never created.

Field name

The default field named is ‘removed’, but you can override it with the PERMANENT_FIELD variable in settings.py:

PERMANENT_FIELD = 'deleted'

Requirements

  • Django 1.7+
  • Python 2.7, 3.4+
Release History

Release History

This version
History Node

1.1.6

History Node

1.1.5

History Node

1.1.4

History Node

1.1.3

History Node

1.1.2

History Node

1.1.1

History Node

1.1.0

History Node

1.0.13

History Node

1.0.12

History Node

1.0.11

History Node

1.0.10

History Node

1.0.9

History Node

1.0.8

History Node

1.0.7

History Node

1.0.6

History Node

1.0.5

History Node

1.0.4

History Node

1.0.3

History Node

1.0.2

History Node

1.0.1

History Node

1.0.0

History Node

0.1.5

History Node

0.1.4

History Node

0.1.3

History Node

0.1.2

History Node

0.1.1

History Node

0.1.0

History Node

0.0.2

History Node

0.0.1

Download Files

Download Files

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

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
django-permanent-1.1.6.tar.gz (9.0 kB) Copy SHA256 Checksum SHA256 Source May 4, 2017

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting