0
0
Djangoframework~30 mins

Admin customization with ModelAdmin in Django - Mini Project: Build & Apply

Choose your learning style9 modes available
Admin customization with ModelAdmin
📖 Scenario: You are building a Django admin interface for a bookstore. You want to customize how the Book model appears in the admin panel to make it easier for staff to manage books.
🎯 Goal: Create a Django ModelAdmin class for the Book model that customizes the admin list display and adds a search box.
📋 What You'll Learn
Create a Book model with fields title (string), author (string), and published_year (integer).
Create a BookAdmin class to customize the admin interface for Book.
In BookAdmin, set list_display to show title, author, and published_year.
Add a search_fields attribute to BookAdmin to enable searching by title and author.
Register the Book model with the BookAdmin class in the admin site.
💡 Why This Matters
🌍 Real World
Customizing the Django admin helps staff manage data easily and efficiently by showing relevant information and search options.
💼 Career
Django developers often customize the admin interface to improve usability for content managers and administrators.
Progress0 / 4 steps
1
Create the Book model
Create a Django model called Book with fields: title as models.CharField(max_length=100), author as models.CharField(max_length=100), and published_year as models.IntegerField().
Django
Need a hint?

Use models.CharField for text fields and models.IntegerField for numbers.

2
Create the BookAdmin class
Create a class called BookAdmin that inherits from admin.ModelAdmin.
Django
Need a hint?

Use class BookAdmin(admin.ModelAdmin): to start your admin customization.

3
Add list_display and search_fields
Inside the BookAdmin class, set list_display to ('title', 'author', 'published_year') and search_fields to ('title', 'author').
Django
Need a hint?

Use tuples for list_display and search_fields with the exact field names.

4
Register Book with BookAdmin
Register the Book model with the admin site using admin.site.register(Book, BookAdmin).
Django
Need a hint?

Use admin.site.register(Book, BookAdmin) to connect the model and admin class.