Add Search Functionality in Admin
A snippet showing how to add search functionality in Django admin.
# models.py
from django.db import models
class Customer(models.Model):
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
email = models.EmailField(unique=True)
phone = models.CharField(max_length=20, blank=True)
def __str__(self):
return f"{self.first_name} {self.last_name}"
# admin.py
from django.contrib import admin
from .models import Customer
@admin.register(Customer)
class CustomerAdmin(admin.ModelAdmin):
list_display = ('first_name', 'last_name', 'email', 'phone')
search_fields = ('first_name', 'last_name', 'email')
Explanation:
-
search_fieldsadds a search box to the admin list view. - Users can search across multiple fields, including text and email fields.
-
Prefix a field with
^to search from the start, or=for exact matches. - Keep the list efficient by only adding fields you really need to search by.
- Category Admin
- Total Views 856
- Last Modified 02 October, 2025
- Tags #admin #search #models #fields
Previous snippet
Customize list_display in Admin
Next snippet