ModelAdmin का Customization
ModelAdmin class वह जगह है जहां आप fine-tune करते हैं कि एक model admin dashboard के अंदर exactly कैसा दिखे और behave करे।In this page:
@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.
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.
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 करें।
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.
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. #}
- Behavior बदलने के लिए
ModelAdminoptions उपयोग करने के बजाय Django के built-in admin templates को directly edit करना। - Customized
ModelAdminclass को register करना भूल जाना, इसलिए default admin interface उपयोग होता है। - Business logic (जैसे emails भेजना) को
save_modelके अंदरsuper().save_model()पहले call किए बिना डालना।
ModelAdminsubclasses आपको एक model के लिएlist_display,search_fields,list_filter, ordering, और ज़्यादा configure करने देते हैं।readonly_fieldsउन fields को मार्क करता है जो form पर दिखते हैं पर edit नहीं किए जा सकते।save_model()को override करना आपको admin के save action में custom logic hook करने देता है।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: