beginner#Django#Admin Interface#Data Management
Using Django's Built-in Admin Interface
Learn how to use Django's built-in admin interface to manage models and data.
Introduction to Django Admin
Django comes with a built-in admin interface that allows you to manage models and data. In this tutorial, we will explore how to use Django's admin interface.
Step 1: Create a Superuser
Create a superuser to access the admin interface:
python manage.py createsuperuser
Step 2: Register Models with the Admin Interface
Register models with the admin interface in admin.py:
from django.contrib import admin
from .models import Book
class BookAdmin(admin.ModelAdmin):
list_display = ('title', 'author')
django.contrib.admin.site.register(Book, BookAdmin)
Step 3: Customize the Admin Interface
Customize the admin interface by adding custom views and templates:
from django.contrib import admin
from .models import Book
class BookAdmin(admin.ModelAdmin):
list_display = ('title', 'author')
def get_queryset(self, request):
return Book.objects.filter(author='John Doe')
Step 4: Use the Admin Interface to Manage Data
Use the admin interface to manage data, including creating, editing, and deleting objects:
# Create a new book
book = Book(title='New Book', author='John Doe')
book.save()
# Edit an existing book
book = Book.objects.get(id=1)
book.title = 'Updated Book'
book.save()
# Delete a book
book = Book.objects.get(id=1)
book.delete()