ปรับแต่ง ModelAdmin
กำหนด list_display, list_filter และ search_fields
ปรับแต่ง ModelAdmin เป็นบทเรียน Django Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Django Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Django Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Beyond the Defaults
The default admin works but feels plain. A ModelAdmin class lets you reshape how each model looks and behaves in the admin.
Subclass and Attach
Create a class that subclasses admin.ModelAdmin, then pass it when you register the model. That class holds all your tweaks.
class PostAdmin(admin.ModelAdmin):
pass
admin.site.register(Post, PostAdmin)Columns in the List
The list page shows one column by default. Set list_display to a tuple of field names and the admin renders each as a column.
list_display = ("title", "author", "created")Filter the Sidebar
Big tables get hard to scan. Add list_filter and the admin shows a sidebar to narrow rows by those fields fast. 🔍
list_filter = ("status", "created")Add a Search Box
Let staff hunt for records by text. Setting search_fields puts a search bar at the top that scans those fields.
search_fields = ("title", "author__name")Click to Edit
By default only the first column links to the edit page. Use list_display_links to choose which columns are clickable instead.
list_display_links = ("title",)Edit Inline in the List
Want to change a value without opening each row? Put a field in list_editable and you can edit it right on the list page.
list_editable = ("status",)Prefill the Slug
Slugs are tedious to type. The prepopulated_fields option fills a slug live from the title as you type it. ⚡
prepopulated_fields = {"slug": ("title",)}Order the Rows
Control the default sort with ordering. A leading minus sorts that field descending, so newest items can lead the list.
ordering = ("-created",)Group the Form
Long edit forms feel cluttered. Use fieldsets to split fields into labeled, collapsible sections on the edit page.
Computed Columns
list_display can also call a method on your admin class. That lets you show derived values, like a short preview of the body.
def short(self, obj):
return obj.body[:30]Quick Check
Which option controls the columns shown on the list page?
Recap
With ModelAdmin you added columns, filters, search, and sorting. A few attributes turn a plain table into a powerful data console. 🛠️
คำถามที่พบบ่อย
บทเรียน “ปรับแต่ง ModelAdmin” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ปรับแต่ง ModelAdmin” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Django Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Django Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ปรับแต่ง ModelAdmin”
กำหนด list_display, list_filter และ search_fields คุณปฏิบัติ Django Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Django Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Django Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “ปรับแต่ง ModelAdmin” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Django Academy นี้ได้ไหม
ได้ บทเรียน Django Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- createsuperuser และการเข้าสู่ระบบ
- ลงทะเบียนโมเดลด้วย admin.site
- ปรับแต่ง ModelAdmin
- อินไลน์และฟิลด์อ่านอย่างเดียว