0
0
Djangoframework~20 mins

Admin customization with ModelAdmin in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Django Admin Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
1:30remaining
What is the effect of list_display in ModelAdmin?
Consider this Django admin customization:
class BookAdmin(admin.ModelAdmin):
    list_display = ('title', 'author', 'published_date')

What will the admin list page for Book show?
AThe list page will show columns for title, author, and published_date fields.
BThe list page will only show the title field as a column.
CThe list page will show all fields of the Book model automatically.
DThe list page will show no columns because list_display is ignored.
Attempts:
2 left
💡 Hint
list_display controls which fields appear as columns in the admin list view.
📝 Syntax
intermediate
1:30remaining
Which ModelAdmin code correctly adds a search box for 'name' field?
You want to add a search box in Django admin to search by the 'name' field of a model. Which code snippet is correct?
A
class MyModelAdmin(admin.ModelAdmin):
    search_fields = ['name']
B
class MyModelAdmin(admin.ModelAdmin):
    search = ['name']
C
class MyModelAdmin(admin.ModelAdmin):
    search_fields = 'name'
D
class MyModelAdmin(admin.ModelAdmin):
    search_fields = ('name')
Attempts:
2 left
💡 Hint
search_fields must be a list or tuple of field names.
🔧 Debug
advanced
2:00remaining
Why does this ModelAdmin code cause an error?
Given this code:
class ProductAdmin(admin.ModelAdmin):
    list_filter = 'category'

What error will Django admin raise when loading this ModelAdmin?
AAttributeError because 'category' field does not exist.
BSyntaxError due to missing parentheses.
CNo error; it works fine showing a filter for category.
DTypeError because list_filter expects a list or tuple, not a string.
Attempts:
2 left
💡 Hint
list_filter must be a list or tuple of field names.
state_output
advanced
2:00remaining
What is the output of this ModelAdmin with custom method in list_display?
Consider this code:
class OrderAdmin(admin.ModelAdmin):
    list_display = ('id', 'customer_name')

    def customer_name(self, obj):
        return obj.customer.first_name + ' ' + obj.customer.last_name
    customer_name.short_description = 'Customer'

What will the admin list page show in the 'Customer' column?
AIt will raise an AttributeError because customer_name is not a model field.
BIt will show the string 'customer_name' literally for all rows.
CIt will show the full name of the customer by combining first and last names.
DIt will show empty cells because customer_name is not defined properly.
Attempts:
2 left
💡 Hint
Custom methods in list_display can show computed values.
🧠 Conceptual
expert
2:30remaining
Which ModelAdmin feature allows grouping fields in the admin form?
You want to organize fields in the Django admin form into sections with titles. Which ModelAdmin attribute do you use?
Asearch_fields
Bfieldsets
Clist_display
Dlist_filter
Attempts:
2 left
💡 Hint
Think about grouping fields with headings in the form view.