Django Admin – using list_filter and search_fields

Django Admin is a great tool to manage metadata from your models.

Let me show you two simple resources: list_filter and search_fields.

ModelAdmin list_filter

Enabled a field filters in the right sidebar, more details.

ModelAdmin search_fields

Enable a search box based on text fields, more details.

Let’s open the admin.py from each module and choose any model. In this case, I will use model Idea.

class IdeaAdmin(admin.ModelAdmin):
    search_fields = ('title', 'detail', 'keywords')
    list_filter = ('content_type', 'status', 'author', 'project')

Below an illustration showing Idea admin page:

Alt text

Complete source code:

from django.contrib import admin
from content.models import Idea

class IdeaAdmin(admin.ModelAdmin):
    search_fields = ("title", "detail", "keywords")
    list_filter = ("content_type", "status", "author", "project")

admin.site.register(Idea, IdeaAdmin)

The admin has multiple hooks for customization. For more details please check the official documentation here.

Reply