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

Admin list_display को Customize करना

list_display decide करता है कि आपके records के admin के table view में कौन से columns दिखें।
Syntax
markup
@admin.register(ModelName)
class ModelNameAdmin(admin.ModelAdmin):
    list_display = ('field1', 'field2', 'field3')

Default List View

बिना किसी customization के, एक model के लिए admin का list page एक column दिखाता है: हर row के लिए __str__() जो भी return करे। यह glance में शायद ही काफी detail होता है।

उदाहरण: The Default List View

With no ModelAdmin customization, Book's list page only shows the __str__ value per row.

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

admin.site.register(Book)
{# 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. #}

`list_display` से Columns जोड़ना

एक ModelAdmin class पर list_display set करना हर named field के लिए एक column जोड़ता है, list page को एक proper table में बदल देते हुए।

उदाहरण: Adding Columns with list_display

The Book list page now shows title, author, and published_date as separate columns.

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

@admin.register(Book)
class BookAdmin(admin.ModelAdmin):
    list_display = ('title', 'author', '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. #}

एक Computed Value Show करना

ModelAdmin (या model खुद) पर defined एक method भी list_display में दिख सकता है, आपको derived information show करने देते हुए जो एक plain field नहीं है।

Note: Method को इसके column header text control करने के लिए एक short_description attribute दें।

उदाहरण: Showing a Computed Value

Adds an 'Recent?' column computed from published_date instead of a stored field.

markup
class BookAdmin(admin.ModelAdmin):
    list_display = ('title', 'is_recent')

    def is_recent(self, obj):
        return obj.published_date.year >= 2020
    is_recent.short_description = 'Recent?'
{# 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. एक many-to-many field को सीधे list_display में डालना, जो Django allow नहीं करता और इसके लिए एक error raise करता है।
  2. वे field names list करना जो model पर मौजूद नहीं हैं, जो एक admin startup error का कारण बनता है।
  3. यह भूल जाना कि list_display सिर्फ list page बदलता है, individual add/edit form को नहीं।
चैप्टर सारांश
  • list_display field names (या method names) का एक tuple है जो admin list page में columns के रूप में show होता है।
  • यह default से unset छोड़े जाने पर model के __str__ output को एक single column के रूप में दिखाने को defaults करता है।
  • Callables और model methods plain field names के साथ list_display में दिख सकते हैं।

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.