views:

176

answers:

3

Hi,

I want to make the following tiny modification to the Django framework. I want it to create a "deleted" field for each model I create, plus of course I want it to be checked as deleted when I delete it from the admin page instead of being physically deleted, and I dont want these records checked as deleted to be listed.

I'm new to Django, I'm seeing if I can do what I want to do with it easily. I need this change because it's the way we currently work.

I admit that I can be doing something stupid by plain ignorance of the framework, plus there can be easiest ways to do what I want to do.

So far these are the changes I have made, I would like to understand how the whole Django framewok works inside but I'm so far from that, is there any documentation online which explains clearly how the inside framework parts/files/modules/classes work together, the specific role of each one, etc

In the base.py file, in the modelbase class, below this code

for obj_name, obj in attrs.items():
new_class.add_to_class(obj_name, obj)

I added

from django.db import models
    new_class.add_to_class('deleted', models.BooleanField())

so now when it creates a model it adds the "deleted" field to it.

In the base.py file, in the save method, I changed what it was there for

self.deleted = True
self.save()

so now it check as deleted a record instead of physically delete it.

Now what I want is those records not to be listed, but I have no idea how or where to do it. Any help is appreciated

+7  A: 

I don't understand why you're modifying the framework code instead of putting your deleted field in a model base class that all of your models extend from.

Nevertheless, a nice way to filter those records out would be to add a custom manager to the model (or your base model class, if you choose to create one). To this manager, override the get_query_set method as described here. In your overridden method, add a exclude(deleted=True) filter.

scompt.com
Thanks, as I said before, I dont have much knowledge of the framework and it was very likely there is going to be a better solution for the problem of adding the field. So I could create a base class with the deleted field and extend my models from it, I didnt think about that, anyway the idea is to always use it this way, but you are right, if you want the possibility to do both you better create a base class instead of touching the framework sourcecode at all
Pablo
+1  A: 
  1. Override the delete() method in your model class, set the deleted attribute there
  2. Create a custom manager which will filter by deleted attribute and set it as the default one (objects = MyDeletedManager)
ohnoes
+4  A: 

Take a look at the Django-logicaldelete app, You just inherit your models from their provided Model class and you get Logical delete for all of them.

It comes with an adminModel as well so you can manage logically deleted models there too.

Jj
I like the look of Django-logicaldelete but I'm concerned that it does something that the django docs say not to do. http://docs.djangoproject.com/en/dev/topics/db/managers/#do-not-filter-away-any-results-in-this-type-of-manager-subclass
StephenPaulger