Admin Search और Filters
search_fields एक search box जोड़ता है, और list_filter एक sidebar जोड़ता है ताकि आप एक लंबी admin list में records को तेज़ी से ढूंढ सकें।@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.
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).
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 करते हैं।
list_filter sidebar को impractically लंबा बना देता है; उनके लिए search_fields prefer करें।उदाहरण: Combining Search and Filters
Gives Book a searchable, filterable admin list in a single ModelAdmin class.
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. #}
- एक ForeignKey field को सीधे
search_fieldsमें डालना double-underscore lookup उपयोग करने के बजाय एक related field में। - सैकड़ों distinct values वाले एक field पर
list_filterजोड़ना, sidebar को unusably लंबा बनाते हुए। - यह उम्मीद करना कि
search_fieldsअपने आप हर field search करेगा -- यह सिर्फ उन fields को search करता है जो आप list करते हैं।
search_fieldsadmin list के ऊपर एक search box जोड़ता है जो listed fields के against match करता है।list_filterlisted fields के लिए clickable filters वाला एक right-hand sidebar जोड़ता है।- Related fields को double-underscore syntax से search किया जा सकता है, जैसे
author__name।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: