Django CRUD Operations

CRUD stands for Create, Read, Update, and Delete. These are the four basic operations used to interact with database records in a Django application. Django’s ORM makes CRUD operations simple by allowing developers to work with Python objects instead of writing SQL queries.

What is CRUD?

  • Create – Add new records to the database.
  • Read – Retrieve records from the database.
  • Update – Modify existing records.
  • Delete – Remove records from the database.

Model Example

from django.db import models

class Post(models.Model):
    title = models.CharField(max_length=200)
    content = models.TextField()

    def __str__(self):
        return self.title

Create Operation

The Create operation inserts new data into the database.

from .models import Post

post = Post.objects.create(
    title="My First Post",
    content="This is a Django CRUD example."
)

Or:

post = Post(
    title="My First Post",
    content="This is a Django CRUD example."
)
post.save()

Read Operation

The Read operation retrieves data from the database.

Get All Records

posts = Post.objects.all()

Get a Single Record

post = Post.objects.get(id=1)

Filter Records

posts = Post.objects.filter(title__icontains="django")

First Record

post = Post.objects.first()

Update Operation

The Update operation modifies existing records.

post = Post.objects.get(id=1)
post.title = "Updated Title"
post.save()

Using QuerySet:

Post.objects.filter(id=1).update(
    title="Updated Title"
)

Delete Operation

The Delete operation removes records from the database.

Delete Single Record

post = Post.objects.get(id=1)
post.delete()

Delete Multiple Records

Post.objects.filter(title="Test").delete()

CRUD Using Function-Based Views

Create View

from django.shortcuts import render, redirect
from .models import Post

def create_post(request):
    if request.method == "POST":
        Post.objects.create(
            title=request.POST['title'],
            content=request.POST['content']
        )
        return redirect('post_list')

    return render(request, 'create.html')

Read View

from django.shortcuts import render
from .models import Post

def post_list(request):
    posts = Post.objects.all()
    return render(request, 'posts.html', {'posts': posts})

Update View

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

def update_post(request, id):
    post = get_object_or_404(Post, id=id)

    if request.method == "POST":
        post.title = request.POST['title']
        post.content = request.POST['content']
        post.save()
        return redirect('post_list')

    return render(request, 'update.html', {'post': post})

Delete View

from django.shortcuts import redirect, get_object_or_404
from .models import Post

def delete_post(request, id):
    post = get_object_or_404(Post, id=id)
    post.delete()
    return redirect('post_list')

Advantages of Django CRUD

  • Simplifies database operations
  • No need to write raw SQL
  • Faster development
  • Secure database interactions
  • Easy maintenance and scalability