5

In the following, I have a Post model. A Post object has a status field that can be 'unpublished' or 'published'.

if status is 'published', I'd like to prevent the object from being deleted, and would like to keep this logic encapsulated in the model itself.

from model_utils import Choices  # from Django-Model-Utils
from model_utils.fields import StatusField


class Post(model.Models)

    STATUS = Choices(
        ('unpublished', _('Unpublished')),
        ('published', _('Published')),
    )

    ...

    status = StatusField(default=STATUS.unpublished)

How can I do this? Overriding the delete method won't work if the objects are being deleted in bulk with a QuerySet. I've read not to use receivers, but I'm not sure why.

StringsOnFire
  • 2,726
  • 5
  • 28
  • 50
  • There is a pre delete event called when you delete via QuerySet.delete(), so listening for it and raising ProtectedError should do the trick. – Todor Jul 17 '16 at 10:03
  • I can't find that in the docs - could you point me to this please? I thought that wouldn't send a signal. – StringsOnFire Jul 17 '16 at 10:24
  • [pre_delete](https://docs.djangoproject.com/en/1.9/ref/signals/#pre-delete). I'm writing from a phone and can't provide a full answer if you don't get one later I can try to provide one. – Todor Jul 17 '16 at 10:28
  • Thanks, I've added an answer based on that – StringsOnFire Jul 17 '16 at 11:14

1 Answers1

10

This is what I have following @Todor's comment:

In signals.py:

from django.db.models import ProtectedError
from django.db.models.signals import pre_delete
from django.dispatch import receiver

from .models import Post

@receiver(pre_delete, sender=Post, dispatch_uid='post_pre_delete_signal')
def protect_posts(sender, instance, using, **kwargs):
    if instance.status is 'unpublished': 
        pass
    else:  # Any other status types I add later will also be protected
        raise ProtectedError('Only unpublished posts can be deleted.')

I welcome improvements or better answers!

StringsOnFire
  • 2,726
  • 5
  • 28
  • 50
  • 2
    Accoring to the [django documentation](https://docs.djangoproject.com/en/2.0/_modules/django/db/models/deletion/) we have to include the object that is being protected, therefore it should be raise ProtectedError('Only unpublished posts can be deleted.', instance) – Mirko Lindner May 06 '18 at 06:57
  • 4
    How do you catch the `ProtectedError`? On my Django 1.11, the exception just bubbles its way to a 500 Server Error. – psaniko Jun 20 '18 at 16:02
  • @psaniko Have you ever found a way to catch the `ProtectedError`? – shadow Aug 17 '21 at 22:58
  • @shadow Don't really remember but I think we had to fix our error handlers or added middleware to catch it. Some SO questions address this directly https://stackoverflow.com/questions/44229783/catch-protected-error-and-show-its-message-to-the-user-instead-of-bare-500-code/44231197 – psaniko Aug 18 '21 at 23:08
  • @psaniko Thank you very much for the reply. I got it to work overwriting the delete_view and the response_action as explained in https://stackoverflow.com/questions/49326282/ – shadow Aug 19 '21 at 00:32