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
2010-09-28 13:35:32
Fantastic, thank you.
kdt
2010-09-29 10:10:50