Bird
Raised Fist0
Djangoframework~20 mins

List display configuration in Django - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Django List Display Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What will be displayed in the Django admin list view?
Given this Django admin configuration, what columns will appear in the list view for the Book model?
Django
from django.contrib import admin
from .models import Book

class BookAdmin(admin.ModelAdmin):
    list_display = ('title', 'author', 'published_date')

admin.site.register(Book, BookAdmin)
AColumns: title, author, published_date
BColumns: title, author only
CColumns: published_date only
DNo columns displayed, default view
Attempts:
2 left
💡 Hint
Check the list_display attribute in the admin class.
📝 Syntax
intermediate
1:30remaining
Identify the syntax error in this list_display configuration
What is wrong with this Django admin list_display setting?
Django
class AuthorAdmin(admin.ModelAdmin):
    list_display = ['name', 'email',]
Alist_display must be a tuple, not a list
BNo syntax error; this is valid Python
CTrailing comma inside list is invalid syntax
DMissing parentheses around list_display
Attempts:
2 left
💡 Hint
Think about Python list syntax and trailing commas.
🔧 Debug
advanced
2:30remaining
Why does this custom method not show in list_display?
This admin class tries to show a custom method in list_display but it does not appear. Why?
Django
class BookAdmin(admin.ModelAdmin):
    list_display = ('title', 'author_name')

    @admin.display(description='Author Name')
    def author_name(self, obj):
        return obj.author.name
ACustom methods cannot be used in list_display
Bauthor_name should be a property, not a method
Clist_display must only contain model field names
DThe method author_name lacks the @admin.display decorator
Attempts:
2 left
💡 Hint
Check Django 3.2+ requirements for custom list_display methods.
❓ state_output
advanced
2:00remaining
What is the effect of setting list_display_links to None?
Consider this admin configuration. What happens to the list view links?
Django
class ArticleAdmin(admin.ModelAdmin):
    list_display = ('title', 'pub_date')
    list_display_links = None
ANo fields will be clickable links to the detail page
BAll fields become clickable links
COnly the first field is clickable link
DRaises a TypeError at runtime
Attempts:
2 left
💡 Hint
list_display_links controls which columns link to the edit page.
🧠 Conceptual
expert
3:00remaining
How to optimize list_display for large datasets?
Which approach best improves Django admin list view performance when using list_display with many related fields?
AUse raw SQL queries instead of Django ORM
BDisable pagination to load all items at once
CUse <code>list_select_related</code> to fetch related objects in one query
DAdd all related fields to list_display to avoid extra queries
Attempts:
2 left
💡 Hint
Think about how to reduce database queries in admin list views.

Practice

(1/5)
1. In Django admin, what does the list_display attribute control?
easy
A. Which fields or methods appear as columns in the admin list view
B. The order of models in the admin site
C. The fields editable in the admin form
D. The database table names for models

Solution

  1. Step 1: Understand the purpose of list_display

    The list_display attribute in Django admin defines which model fields or methods are shown as columns in the list view of the admin interface.
  2. Step 2: Differentiate from other admin attributes

    Other attributes like ordering control order, fields control form fields, and database table names are unrelated to list_display.
  3. Final Answer:

    Which fields or methods appear as columns in the admin list view -> Option A
  4. Quick Check:

    list_display = columns shown [OK]
Hint: Remember: list_display = columns in admin list view [OK]
Common Mistakes:
  • Confusing list_display with ordering or form fields
  • Thinking list_display changes database schema
  • Assuming list_display controls permissions
2. Which of the following is the correct way to set list_display in a Django admin class?
easy
A. list_display = 'field1, field2, method_name'
B. list_display = (field1, field2, method_name)
C. list_display = {'field1', 'field2', 'method_name'}
D. list_display = ['field1', 'field2', 'method_name']

Solution

  1. Step 1: Identify the correct data type for list_display

    The list_display attribute expects a list or tuple of strings naming fields or methods. Both list and tuple are valid, but list is most common.
  2. Step 2: Analyze each option's syntax

    list_display = ['field1', 'field2', 'method_name'] uses a list with square brackets and strings, which is correct. list_display = (field1, field2, method_name) uses tuple syntax but lacks quotes around the names, treating them as undefined variables (NameError). list_display = {'field1', 'field2', 'method_name'} uses a set, which is unordered and invalid here. list_display = 'field1, field2, method_name' is a string, which is incorrect.
  3. Final Answer:

    list_display = ['field1', 'field2', 'method_name'] -> Option D
  4. Quick Check:

    list_display uses list syntax [OK]
Hint: Use square brackets for list_display fields [OK]
Common Mistakes:
  • Using a string instead of a list or tuple
  • Using a set which is unordered
  • Forgetting quotes around field names
3. Given this Django admin code snippet, what columns will appear in the admin list view?
class ProductAdmin(admin.ModelAdmin):
    list_display = ['name', 'price', 'discounted_price']

    def discounted_price(self, obj):
        return obj.price * 0.9
medium
A. Columns: name, price, discounted_price but discounted_price shows an error
B. Columns: name, price only; discounted_price is ignored
C. Columns: name, price, discounted_price showing name, price, and 90% of price
D. Only discounted_price column appears

Solution

  1. Step 1: Understand list_display with methods

    The list_display includes 'discounted_price', which is a method defined in the admin class. Django calls this method for each object to show the computed value.
  2. Step 2: Analyze the method's return value

    The method returns 90% of the product's price, so the column will show the discounted price correctly.
  3. Final Answer:

    Columns: name, price, discounted_price showing name, price, and 90% of price -> Option C
  4. Quick Check:

    Method in list_display shows computed column [OK]
Hint: Methods in list_display show computed columns [OK]
Common Mistakes:
  • Assuming methods in list_display are ignored
  • Forgetting to define the method in admin
  • Method returning wrong or no value
4. What is wrong with this Django admin code?
class OrderAdmin(admin.ModelAdmin):
    list_display = ['id', 'total_price', 'calculate_tax']

    def calculate_tax(self):
        return self.total_price * 0.1
medium
A. calculate_tax should be a property, not a method
B. The method calculate_tax is missing the obj parameter
C. total_price is not a valid field
D. list_display should not include methods

Solution

  1. Step 1: Check method signature for list_display methods

    Methods used in list_display must accept the model instance as the first parameter, usually named obj. Here, calculate_tax has no parameters, so it will cause an error.
  2. Step 2: Confirm other options

    Methods can be in list_display, total_price is assumed valid, and methods do not have to be properties.
  3. Final Answer:

    The method calculate_tax is missing the obj parameter -> Option B
  4. Quick Check:

    list_display methods need obj parameter [OK]
Hint: list_display methods must accept obj parameter [OK]
Common Mistakes:
  • Defining method without obj parameter
  • Thinking methods can't be in list_display
  • Confusing method with property decorator
5. You want to show a custom column in Django admin that displays 'Yes' if a product's stock is above 0, otherwise 'No'. Which is the correct way to add this to list_display?
hard
A. Define a method in admin: def in_stock(self, obj): return 'Yes' if obj.stock > 0 else 'No' and add 'in_stock' to list_display
B. Add 'in_stock' to list_display without defining a method
C. Override the model's __str__ method to return 'Yes' or 'No'
D. Use a property on the model named in_stock

Solution

  1. Step 1: Create a method in admin for custom display

    To show a custom column based on logic, define a method in the admin class that takes self and obj and returns the desired string.
  2. Step 2: Add the method name as a string to list_display

    Adding the method name to list_display tells Django to call it for each row and show the result.
  3. Final Answer:

    Define a method in admin: def in_stock(self, obj): return 'Yes' if obj.stock > 0 else 'No' and add 'in_stock' to list_display -> Option A
  4. Quick Check:

    Custom columns need admin methods + list_display [OK]
Hint: Custom columns need admin methods and list_display entry [OK]
Common Mistakes:
  • Adding method name without defining it
  • Trying to use model __str__ for columns
  • Assuming properties on model auto-show in list_display
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard