views:

27

answers:

1

I have a model whose delete() method I have overridden. Because this is not called on a bulk QuerySet delete, I would like to disable QuerySet deletion or somehow assert against it happening. Is there any way to do that?

+4  A: 

You can create a custom QuerySet class and use it in a manager, e.g.:

from django.db import models

class NoDeleteQuerySet(models.query.QuerySet):
    def delete(self):
        raise NotImplementedError("can't delete this queryset")

class NoDeleteQuerySetManager(models.Manager):
    def get_query_set(self):
        return NoDeleteQuerySet(self.model)

class MyModel(model.Model):
    objects = NoDeleteQuerySetManager()
piquadrat
Fantastic, thank you.
kdt