Admin list_display को Customize करना
list_display decide करता है कि आपके records के admin के table view में कौन से columns दिखें।@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.
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.
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 नहीं है।
short_description attribute दें।उदाहरण: Showing a Computed Value
Adds an 'Recent?' column computed from published_date instead of a stored field.
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. #}
- एक many-to-many field को सीधे
list_displayमें डालना, जो Django allow नहीं करता और इसके लिए एक error raise करता है। - वे field names list करना जो model पर मौजूद नहीं हैं, जो एक admin startup error का कारण बनता है।
- यह भूल जाना कि
list_displayसिर्फ list page बदलता है, individual add/edit form को नहीं।
list_displayfield 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में दिख सकते हैं।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: