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

ModelAdmin का Customization

एक ModelAdmin class वह जगह है जहां आप fine-tune करते हैं कि एक model admin dashboard के अंदर exactly कैसा दिखे और behave करे।
Syntax
markup
@admin.register(ModelName)
class ModelNameAdmin(admin.ModelAdmin):
    list_display = ('field1', 'field2')
    ordering = ('field1',)
    readonly_fields = ('field3',)

एक ModelAdmin Subclass क्यों उपयोग करें

सिर्फ admin.site.register(Model) आपको Django के defaults देता है। Ordering बदलने, read-only fields जोड़ने, या saving में hook करने के लिए, आप admin.ModelAdmin को subclass करते हैं और इसके बजाय इसे register करते हैं।

उदाहरण: Why Use a ModelAdmin Subclass

Shows the newest books first in the admin list page.

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

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

Fields को Read-Only बनाना

readonly_fields उन fields को list करता है जो अब भी add/edit form पर दिखते हैं पर वहां change नहीं किए जा सकते -- timestamps या computed values के लिए useful है।

उदाहरण: Making Fields Read-Only

created_at is displayed on the edit form but grayed out and non-editable.

markup
class BookAdmin(admin.ModelAdmin):
    readonly_fields = ('created_at',)
{# 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. #}

`save_model` में Hook करना

save_model() को override करना आपको हर बार admin से एक record save होने पर code run करने देता है, जब तक आप इसे असल में persist करने के लिए अब भी parent implementation call करें।

Warning: super().save_model(request, obj, form, change) skip करने का मतलब है object कभी असल में database में save नहीं होता।

उदाहरण: Hooking Into save_model

Trims whitespace from the title every time a Book is saved through the admin.

markup
class BookAdmin(admin.ModelAdmin):
    def save_model(self, request, obj, form, change):
        obj.title = obj.title.strip()
        super().save_model(request, obj, form, change)
{# 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. Behavior बदलने के लिए ModelAdmin options उपयोग करने के बजाय Django के built-in admin templates को directly edit करना।
  2. Customized ModelAdmin class को register करना भूल जाना, इसलिए default admin interface उपयोग होता है।
  3. Business logic (जैसे emails भेजना) को save_model के अंदर super().save_model() पहले call किए बिना डालना।
चैप्टर सारांश
  • ModelAdmin subclasses आपको एक model के लिए list_display, search_fields, list_filter, ordering, और ज़्यादा configure करने देते हैं।
  • readonly_fields उन fields को मार्क करता है जो form पर दिखते हैं पर edit नहीं किए जा सकते।
  • save_model() को override करना आपको admin के save action में custom logic hook करने देता है।

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.