Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django disable model delete

Tags:

python

django

So every model comes with some commonly used functions such as save and delete.

Delete is often overridden to set a boolean field such as is_active to false, this way data is not lost. But sometimes a model exists that has information that, once created, should always exist and never even be "inactive". I was wondering what the best practice for handling this model's delete method would be?

ideas

make it simply useless:

def delete(self):
    return False

but that just seems odd. Is there maybe a Meta option to disable deleting? is there any "nice" way to do this?

like image 605
Ryan Saxe Avatar asked Nov 02 '22 08:11

Ryan Saxe


1 Answers

Well it depends, you cannot truly restrict deletion, because somebody can always call delete() on queryset or just plain DELETE sql command. If you want to disable delete button in django admin though, you should look here.

like image 124
Dmitry Shevchenko Avatar answered Nov 08 '22 10:11

Dmitry Shevchenko