Instance Manipulate¶
ParanoidModel has some some differences on default Django methods.
Save¶
This method has no difference, it work just like Django's
1 2 |
|
Delete¶
The most important method. This is why pararanoid model exists. When delete()
an instance it should not be really deleted from database, but hide from user.
The magic is in the attribute deleted_at
. When there is no date (deleted_at is None) it means it has not been deleted, but if has a date it means it has been deleted. So when we call delete(), it will set up the current date to delete_at field and save the instance, instead of deleted.
Warning
The delete works on CASCADE . That means all related objects are going to be soft deleted as well
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
delete(hard_delete=True)¶
You can also delete datas from database.
If you really wants to delete the instance from databse you can use parameter hard_delete
. It calls Django's default method
1 2 3 4 |
|
Danger
Be careful using hard_delete
.
If hard_delete
is True, it will call Django's default delete method, so instance will be deleted from database.
is_soft_deletd¶
This is a @property that returns a boolean if current instance has been soft deleted or not. Otherwise, it returns if attribute deleted_at is None. It is just a more easy way to check if deleted_at is None instead of use this whole sentence to check.
So you can just do the following:
1 2 3 4 5 6 7 8 |
|
This property is useful it in a code like this:
1 2 3 4 |
|
Restore¶
Once an instance has been soft deleted, it can be easily undeleted with method restore()
1 2 3 4 5 6 7 8 9 |
|