Soft Deletion Implementation: The Contact
model has a custom delete
method that overrides the default delete
method. Instead of permanently deleting the contact, this method sets the is_deleted
field to True
and saves the object. The soft-deletion logic is encapsulated within this method.
def delete(self, using=None, keep_parents=False):
self.is_deleted = True
self.save()
Views: The project includes several views in the views.py
file:
- contact_list: Renders the list of contacts, excluding those marked as soft-deleted (
is_deleted=False
). - contact_detail: Displays detailed information for a specific contact.
- soft_delete_contact: Marks a contact as soft-deleted when a POST request is received. Redirects to the contact list after soft deletion.
- create_contact: Handles both GET and POST requests for creating a new contact. On a POST request, a new contact is created, and the user is redirected to the contact list.
Templates: The templates are located in the contacts
directory and include:
- contact_list.html: Displays a list of contacts.
- contact_detail.html: Shows detailed information for a specific contact.
- create_contact.html: Form for creating a new contact.
API Call: The requests
library is imported to facilitate potential integration with external APIs or services, though this feature is not implemented in the current code.
Soft Deletion in Action: The key feature of this project is the soft deletion of contacts. When a contact is marked for deletion using the soft_delete_contact
view, it is not immediately removed from the database. Instead, the is_deleted
field is set to True
. This approach allows for the recovery of deleted contacts and maintains a more audit-friendly data history.
Feel free to explore and customize this project for your specific needs. The soft deletion mechanism can be extended to include additional functionality, such as a trash bin or recovery options for deleted contacts.
5 reviews for Soft Deletion Project in Django | Apycoder
There are no reviews yet.