CRUD Operations in Django Admin
Last Updated :
21 May, 2025
Django Admin is a built-in tool that automatically generates a web interface for your models. Instead of building separate pages or forms, Django Admin handles:
Prerequisites: Django Admin Interface
In this article, we will create a simple Django project named projectName with an app called app. We will define a real-world example model called Book, representing books in a library, and perform CRUD operations using the Django Admin panel.
Creating the App and Model
Step 1: Create Your Django Project and App
After creating the app, make sure to add your app to the INSTALLED_APPS list in settings.py file of projectName.
Step 2: Define the Book Model
In app/models.py, define a Book model to represent books in your library. This model will include common book details like title, author, published date, ISBN, number of pages, and availability status.
Python
from django.db import models
from django.db import models
class Book(models.Model):
title = models.CharField(max_length=200)
author = models.CharField(max_length=100)
published_date = models.DateField()
isbn = models.CharField(max_length=13, unique=True)
pages = models.IntegerField()
available = models.BooleanField(default=True)
def __str__(self):
return f"{self.title} by {self.author}"
Step 3: Migrate Your Database
After defining the model, apply the changes to your database by running the following commands in the terminal:
python manage.py makemigrations
python manage.py migrate
This creates the necessary tables for your new Book model.
Step 4: Register the Model in Django Admin
To manage Book records via the admin panel, register your model in app/admin.py:
Python
from django.contrib import admin
from django.contrib import admin
from .models import Book
@admin.register(Book)
class BookAdmin(admin.ModelAdmin):
list_display = ('title', 'author', 'published_date', 'isbn', 'available')
search_fields = ('title', 'author', 'isbn')
list_filter = ('available', 'published_date')
This customizes the admin interface to show important book details and helps you find books faster using search and filters.
Step 5: Create a Superuser to Access Admin
Create a superuser account to log into the admin panel:
python manage.py createsuperuser
Follow the prompts to set your username, email, and password. After creating the superuser, you will be to access the admin panel by logging in using the credentials.
Step 6: Run the Development Server
Start the Django server:
python manage.py runserver
After the development server is running, open your web browser and go to: http://127.0.0.1:8000/admin/ and log in with your superuser credentials.
Snapshot of admin panel1. Create New Book Records
- Click Books in the admin sidebar.
- Click Add Book.
- Fill in details like title, author, and ISBN.
- Click Save to add the book.
Adding new book2. View (Read) Books
- The Books list page shows all saved books.
- Use the search bar or filters on the right to find specific books quickly.
Viewing all entries 3. Edit Existing Books
- Click on a book title from the list.
- Update any details in the form.
- Click Save to apply changes.
Editing existing entry4. Delete Books
- Select the book/books.
- Select the DELETE option from the dropdown above the list of books.
- Confirm to permanently remove the book.
Deleting Records
Similar Reads
Python Django Projects with Source Code (Beginners to Advanced) Python Django Projects with Source Code - Adding a project portfolio to your resume helps to show your skills and potential to your recruiter. Because in the tech space, real-time project experience matters a lot. Now, to make your resume powerful, we have come up with the top Django projects with s
5 min read
Django Models A Django model is a Python class that represents a database table. Models make it easy to define and work with database tables using simple Python code. Instead of writing complex SQL queries, we use Djangoâs built-in ORM (Object Relational Mapper), which allows us to interact with the database in a
8 min read
get_object_or_404 method in Django Models Some functions are hard as well as boring to code each and every time. But Django users don't have to worry about that because Django has some awesome built-in functions to make our work easy and enjoyable. Let's discuss get_object_or_404() here. What is get_object_or_404 in Django?get_object_or_404
2 min read
Django Function Based Views Django is a Python-based web framework which allows you to quickly create web application without all of the installation or dependency problems that you normally will find with other frameworks. Django is based on MVT (Model View Template) architecture and revolves around CRUD (Create, Retrieve, Up
7 min read
Difference Between ASGI and WSGI in Django ASGI (Asynchronous Server Gateway Interface) and WSGI (Web Server Gateway Interface), as the name suggests, both act as bridges between Web Servers and our Python web applications. They are the Python specifications that define how web servers and web applications will interact with each other. Unde
2 min read
Django model data types and fields list Django models represent the structure of your database tables, and fields are the core components of those models. Fields define the type of data each database column can hold and how it should behave. This article covers all major Django model field types and their usage.Defining Fields in a ModelE
4 min read
ImageField - Django Models ImageField is a specialized version of Django's FileField designed to handle image uploads. It restricts uploads to image formats and provides additional attributes for storing image dimensions.Default form widget: ClearableFileInputRequires: Pillow library for image processingInstall Pillow with: p
3 min read
Top 10 Django Projects For Beginners With Source Code When it comes to software development in general, all development is run by websites on the internet. Even when you arenât actively looking for web development or a Full stack developer role, having worked on Django Projects or any web development projects will substantially improve your portfolio r
9 min read
Django Class Based Views Class-Based Views (CBVs) allow developers to handle HTTP requests in a structured and reusable way. With CBVs, different HTTP methods (like GET, POST) are handled as separate methods in a class, which helps with code organization and reusability.Advantages of CBVsSeparation of Logic: CBVs separate d
6 min read
DateTimeField - Django Models DateTimeField is a date and time field which stores date, represented in Python by a datetime.datetime instance. As the name suggests, this field is used to store an object of datetime created in python. The default form widget for this field is a TextInput. The admin uses two separate TextInput wid
5 min read