MnogoByte / django-permanent

Yet another approach to creation of non-deletable and non-removable from db restorable django models.
71 stars 20 forks source link

ModelAdmin #64

Closed cancan101 closed 7 years ago

cancan101 commented 7 years ago

Might be cool to show example / offer a ModelAdmin for PermanentModel which support un-deleting. Would need to modify the queryset used in admin: http://stackoverflow.com/questions/1545067/django-specify-which-model-manager-django-admin-should-use and perhaps some way of adding an un-delete action.

meteozond commented 7 years ago

As for me, I don't like the idea allowing users to operate removed objects due to problems of related resolving and sufficient restoring.

But if you really need... It is better to provide it though the separate interface - some kind of recycle bin. You can create separate proxy model with DeletedQuerySet as default:

models.py

class MyModel(PermanentModel):
    # Some fields
    pass

# Needed by `delete_selected` default action
class MyDeletedQuerySet(DeletedQuerySet):
    def delete(self, *args, **kwargs):
        super(MyDeletedQuerySet, self).__init__(force=True, *args, **kwargs)

class MyDeletedModel(MyModel)
    objects = QuerySetManager(MyDeletedQuerySet)
    _base_manager = QuerySetManager(MyDeletedQuerySet)
    class Meta:
        proxy=True
    def delete(self, *args, **kwargs):
        super(MyDeletedModel, self).__init__(force=True, *args, **kwargs)

Separate model admin with restore action:

admin.py

class MyDeletedModelAdmin(ModelAdmin):
    actions = ['restore', ]
    def restore(self, request, queryset):
        queryset.restore()

admin.site.register(MyDeletedModel, MyDeletedModelAdmin)
bashu commented 5 years ago

@cancan101 try https://github.com/bashu/django-permanent-helpers