Skip to content

BikramjeetSingh/Django-Soft-Delete-Tutorial-Example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Complete source code for the example in my article on implementing soft deletion in Django.

Setup

  1. Clone repository
  2. (Optional but recommended) Create a virtual environment, python -m venv venv, and activate it, source venv/bin/activate
  3. Install requirements, pip install -r requirements.txt
  4. Create database and run migrations: python manage.py migrate
  5. Run the Django shell: python manage.py shell

Try Out the Soft Deletion Pattern

Import the relevant models:

from django.contrib.auth.models import User
from tutorialapp.models import Note

Create a user:

john = User.objects.create_user('john', 'lennon@thebeatles.com', 'johnpassword')

Create a couple of notes:

my_note = Note.objects.create(user=john, title="Strawberry Fields", content="Strawberry Fields Forever")
another_note = Note.objects.create(user=john, title="Here Comes The Sun", content="It's All Right")

Soft delete them:

my_note.soft_delete()

And restore them:

my_note.restore()

Fetch undeleted notes:

Note.objects.all()

Fetch all notes, included those that have been soft deleted:

Note.all_objects.all()

Fetch undeleted notes belonging to a user, using Django's reverse relationships:

john.notes.all()

About

Complete source code for the example in my article on soft deletion in Django.

Resources

Stars

Watchers

Forks

Languages