Chapter 4: Django Admin – Set Fields to Display

Django Admin – Set Fields to Display: How to control exactly which fields appear in the list view (also called the change list, the table view you see when you click on “Questions” or “Choices”)

Right now if you only did admin.site.register(Question) you see something very basic and ugly:

  • id
  • question_text (maybe truncated badly)
  • pub_date
  • … and nothing else useful

Today we’re going to make that list beautiful, informative, clickable, filter-friendly, and production-ready — step by step, very slowly, like I’m explaining it to you in person.

We’ll use your existing Question and Choice models.

Goal – What a Nice Admin List Should Have

Before we write code, let’s agree what we want to see when we open “Questions”:

  • Shortened question text (clickable to edit)
  • Category with color
  • Publication date
  • “Recent?” icon (green/red)
  • Active/Inactive status icon
  • Total vote count (aggregated)
  • Slug (for debugging)
  • Maybe edit inline for is_active

Same idea for Choices.

Step 1 – The Minimal “list_display” (Start Here)

Open polls/admin.py

Replace with this clean base:

Python

→ Refresh admin → already better than default (id gone, useful fields)

But still boring. Let’s level it up.

Step 2 – Make It Really Nice (Full Recommended Setup)

Replace the whole QuestionAdmin class with this:

Python

And for ChoiceAdmin:

Python

What You Just Gained (Refresh Admin → Questions List)

  • Shortened question text (clickable to edit)
  • Category in nice colors
  • Green check / red X for recent
  • Active/Inactive badge
  • Total votes column (real aggregation!)
  • Slug preview
  • Clean, scannable, informative

Step 3 – Quick Explanations of Important Tricks

Line / Concept Why it’s useful
list_display Defines columns in the table
list_display_links Which columns are clickable (usually the title)
@admin.display(description=…) Custom column header (instead of method name)
ordering=… Sort column when clicking header
boolean=True Shows nice green check / red X icon instead of True/False
format_html(…) Safely inject HTML (color, links, icons)
aggregate(total=Sum(‘votes’)) Calculate total votes across related choices
raw_id_fields Better foreign-key widget when parent table is huge

Step 4 – Test Drive (Do This Right Now)

  1. Go to /admin/ → Questions
  2. See nice columns, colors, icons, vote counts
  3. Click column headers → sort
  4. Use search box → type part of question text
  5. Use filters (left or right sidebar) → show only active, only fun category
  6. Add new question → see slug auto-fill

Bonus – Make It Even Better (Optional but Loved)

Add this to QuestionAdmin:

Python

→ Now you can toggle is_active directly in the list without opening edit page.

Common “Why Doesn’t It Show?” Problems

Problem Likely Cause Fix
Column shows method name not value Forgot @admin.display(description=…) Add the decorator
Vote count always 0 No related choices or wrong aggregation Check choices__votes__sum syntax
No color / HTML Used str() instead of format_html Use format_html()
Clickable column not working Forgot list_display_links Add (‘question_text_short’,)
Old data not updated Cache or browser refresh Ctrl+F5 or incognito

Your Quick Homework (Do This – It Will Stick)

  1. Copy-paste the full QuestionAdmin and ChoiceAdmin code above
  2. Refresh admin → Questions list
  3. Create 4–5 questions with different categories & vote counts
  4. Play with search, filters, sorting, click columns
  5. Notice how much more useful the list becomes

Tell me what you want next:

  • “Looks great! Now show me how to add custom admin actions (bulk publish/unpublish)”
  • “How to make some columns read-only or editable inline?”
  • “I want to add charts or summary stats on the change list”
  • “Got weird column / error – here’s what I see”
  • Or finally ready for: “Let’s finish the public voting flow – form + POST + F() + results”

You now have a clean, powerful, informative admin list view — exactly what real teams use every day.

You’re doing excellent work — let’s keep going! 🚀🇮🇳

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *