How to Implement Soft Delete in Django Application

Oct. 14, 2023


0
4 min read
1.55K

In many applications, it's essential to keep a history of records and provide the ability to recover deleted data. One way to achieve this is by implementing soft delete, a technique where records are marked as deleted instead of physically removing them from the database. This article will guide you through the process of adding soft delete functionality to your Django application.

Step 1: Create a New Field for Soft Delete

The first step in implementing soft delete is to create a new field in your Django model that will be used to mark records as deleted. This field is typically a BooleanField, which is set to True when a record is deleted and False when it's not. Here's an example using a Post model:

from django.db import models

class Post(models.Model):
    title = models.CharField(max_length=100)
    content = models.TextField()
    deleted = models.BooleanField(default=False)

In this example, we've added a deleted field to the Post model to indicate whether a post is soft-deleted or not.

Step 2: Override the Model's Manager

To ensure that soft-deleted records are filtered out by default, create a custom manager for your model. This manager will override the get_queryset method to filter out deleted records:

class PostManager(models.Manager):
    def get_queryset(self):
        return super().get_queryset().filter(deleted=False)

class Post(models.Model):
    title = models.CharField(max_length=100)
    content = models.TextField()
    deleted = models.BooleanField(default=False)

    objects = PostManager()

By using this custom manager, any queries made with Post.objects will automatically exclude soft-deleted records. This ensures that deleted records are not retrieved in standard queries.

Step 3: Override the delete Method

The next step is to override the delete method in your model to mark records as deleted instead of physically removing them. To do this, set the deleted field to True and call the save method:

class Post(models.Model):
    title = models.CharField(max_length=100)
    content = models.TextField()
    deleted = models.BooleanField(default=False)

    objects = PostManager()

    def delete(self):
        self.deleted = True
        self.save()

Now, when you call post_instance.delete(), it will set the deleted field to True, effectively soft-deleting the record. This soft delete operation allows you to keep the historical data intact while marking it as deleted.

Step 4: Implement Function-Based Views

To fully integrate soft delete into your application, create function-based views that handle CRUD operations and soft delete actions. Here's an example of how to create views for a Post model:

from django.shortcuts import render, get_object_or_404, redirect
from .models import Post
from .forms import PostForm

def post_list(request):
    # Retrieve and display non-deleted posts
    posts = Post.objects.all()
    return render(request, 'blog/post_list.html', {'posts': posts})

def post_detail(request, pk):
    # Display the details of a post
    post = get_object_or_404(Post, pk=pk)
    return render(request, 'blog/post_detail.html', {'post': post})

def post_create(request):
    if request.method == 'POST':
        form = PostForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('post_list')
    else:
        form = PostForm()
    return render(request, 'blog/post_form.html', {'form': form})

def post_edit(request, pk):
    post = get_object_or_404(Post, pk=pk)
    if request.method == 'POST':
        form = PostForm(request.POST, instance=post)
        if form.is_valid():
            form.save()
            return redirect('post_list')
    else:
        form = PostForm(instance=post)
    return render(request, 'blog/post_form.html', {'form': form})

def post_delete(request, pk):
    post = get_object_or_404(Post, pk=pk)
    if request.method == 'POST':
        post.delete()
        return redirect('post_list')
    return render(request, 'blog/post_confirm_delete.html', {'post': post})

In the example above, we have created function-based views for listing, creating, editing, and deleting posts. The post_delete view uses the overridden delete method to soft-delete the post. Make sure to update your URL patterns accordingly to map these views.

Step 5: Update Your Views

To fully integrate soft delete into your application, you'll need to update your views. Modify your list views to show only non-deleted records by default and provide actions to "delete" or "undelete" records as needed.

You may also want to create a separate "Trash" view where users can access their soft-deleted records. Make sure to filter the records with deleted=True in that view.

Step 6: Migrate Your Database

After making these changes to your model, create and apply a migration to update your database schema to include the deleted field:

python manage.py makemigrations
python manage.py migrate

Now, your Django application should have soft delete functionality in place. Deleted records will be marked as such, and you can easily filter them out in your queries using the custom manager. If you implement the "undelete" functionality, users can also restore deleted records, ensuring that data can be managed and recovered effectively.

Implementing soft delete in your Django application is a powerful way to maintain data integrity and provide a safety net for recovering accidentally deleted records without the need for complicated data restoration procedures.

django Manager databse Delete Soft-Delete Appreciate you stopping by my post! 😊

Add a comment


Note: If you use these tags, write your text inside the HTML tag.
Login Required
Author's profile
Profile Image

Abdulla Fajal

Django Developer

With 'espere.in' under my care, I, Abdulla Fajal, graciously invite your insights and suggestions, as we endeavour to craft an exquisite online experience together.