Replacing delete, with hidden
One fairly common thing on a completed application is the request to hide rows from the database, rather than delete.
Just today I had this problem, and this was solved in just a couple of lines.
These are standard Django functions, nothing custom.
My current model (for example only)
class Category(models.Model):
title = models.CharField(max_length=100, unique=True)
My new model
class Category(models.Model):
title = models.CharField(max_length=100, unique=True)
deleted = models.BooleanField(db_index=True)
objects = NotDeleted()
show_all = models.Manager()
What is this NotDeleted
?, this is where the magic happens.
class NotDeleted(models.Manager):
def get_query_set(self):
return super(NotDeleted, self).get_query_set().filter(deleted=False)
This will automatically append deleted=False
to your database query.
If for some reason you need to get deleted items again,
categories = Category.show_all.all()
You could create a separate manager, just to show deleted items as well.
For more information on Django database managers, http://docs.djangoproject.com/en/1.0/topics/db/managers/#modifying-initial-manager-querysets