← Back to Django Course | Chapter 8: Django Admin | Lesson 5 of 8

Admin Search और Filters

search_fields एक search box जोड़ता है, और list_filter एक sidebar जोड़ता है ताकि आप एक लंबी admin list में records को तेज़ी से ढूंढ सकें।
Syntax
markup
@admin.register(ModelName)
class ModelNameAdmin(admin.ModelAdmin):
    search_fields = ('field1', 'field2')
    list_filter = ('field3',)

एक Search Box जोड़ना

एक ModelAdmin पर search_fields set करना list page के ऊपर एक search box डालता है जो हर listed field के against एक case-insensitive partial match perform करता है।

उदाहरण: Adding a Search Box

Typing into the search box now matches against Book.title and the related Author's name field.

markup
from django.contrib import admin
from .models import Book

@admin.register(Book)
class BookAdmin(admin.ModelAdmin):
    search_fields = ('title', 'author__name')
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

Filter Sidebar Options जोड़ना

list_filter clickable filters का एक sidebar जोड़ता है। यह उन fields पर best काम करता है जिनके पास values का एक small, fixed set हो जैसे booleans, choices, या dates।

उदाहरण: Adding Filter Sidebar Options

Adds sidebar filters for is_published (True/False) and published_date (by year/month/day).

markup
class BookAdmin(admin.ModelAdmin):
    list_filter = ('is_published', 'published_date')
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

Search और Filters Combine करना

search_fields और list_filter साथ और independently काम करते हैं -- एक search query और एक active filter दोनों एक साथ उसी list को narrow करते हैं।

Warning: कई unique values वाले field (जैसे एक free-text title) पर list_filter sidebar को impractically लंबा बना देता है; उनके लिए search_fields prefer करें।

उदाहरण: Combining Search and Filters

Gives Book a searchable, filterable admin list in a single ModelAdmin class.

markup
class BookAdmin(admin.ModelAdmin):
    list_display = ('title', 'author', 'is_published')
    search_fields = ('title',)
    list_filter = ('is_published',)
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक ForeignKey field को सीधे search_fields में डालना double-underscore lookup उपयोग करने के बजाय एक related field में।
  2. सैकड़ों distinct values वाले एक field पर list_filter जोड़ना, sidebar को unusably लंबा बनाते हुए।
  3. यह उम्मीद करना कि search_fields अपने आप हर field search करेगा -- यह सिर्फ उन fields को search करता है जो आप list करते हैं।
चैप्टर सारांश
  • search_fields admin list के ऊपर एक search box जोड़ता है जो listed fields के against match करता है।
  • list_filter listed fields के लिए clickable filters वाला एक right-hand sidebar जोड़ता है।
  • Related fields को double-underscore syntax से search किया जा सकता है, जैसे author__name।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.