Bird
Raised Fist0
Djangoframework~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of customizing a Django ModelAdmin class?
easy
A. To define URL routes for the model views
B. To control how a model is displayed and managed in the Django admin interface
C. To write custom SQL queries for the model
D. To create new database tables automatically

Solution

  1. Step 1: Understand ModelAdmin role

    The ModelAdmin class customizes the admin interface for a model, such as which fields show and how data is filtered.
  2. Step 2: Identify what ModelAdmin does not do

    It does not create tables, write SQL, or define URLs; those are handled elsewhere in Django.
  3. Final Answer:

    To control how a model is displayed and managed in the Django admin interface -> Option B
  4. Quick Check:

    ModelAdmin controls admin display = D [OK]
Hint: ModelAdmin customizes admin display, not database or URLs [OK]
Common Mistakes:
  • Confusing ModelAdmin with database migrations
  • Thinking ModelAdmin handles URL routing
  • Assuming ModelAdmin writes SQL queries
2. Which of the following is the correct way to register a model with a custom ModelAdmin in Django admin?
easy
A. admin.site.add(MyModel, MyModelAdmin)
B. admin.register(MyModel, MyModelAdmin)
C. admin.site.register(MyModel, MyModelAdmin)
D. admin.register_site(MyModel, MyModelAdmin)

Solution

  1. Step 1: Recall Django admin registration syntax

    The correct method to register a model with a custom admin class is admin.site.register(Model, ModelAdmin).
  2. Step 2: Check other options for validity

    Other options like admin.register or admin.site.add do not exist in Django's admin API.
  3. Final Answer:

    admin.site.register(MyModel, MyModelAdmin) -> Option C
  4. Quick Check:

    Use admin.site.register to register models = A [OK]
Hint: Use admin.site.register(Model, AdminClass) to register [OK]
Common Mistakes:
  • Using admin.register instead of admin.site.register
  • Trying to call admin.site.add which doesn't exist
  • Confusing method names for registration
3. Given this ModelAdmin code, what fields will appear in the Django admin list view?
class BookAdmin(admin.ModelAdmin):
    list_display = ('title', 'author', 'published_date')

admin.site.register(Book, BookAdmin)
medium
A. 'title', 'author', and 'published_date' fields
B. Only 'title' and 'author' fields
C. All fields of the Book model
D. No fields will appear because list_display is incorrect

Solution

  1. Step 1: Understand list_display usage

    The list_display attribute defines exactly which fields show in the admin list view.
  2. Step 2: Check the fields listed

    The tuple includes 'title', 'author', and 'published_date', so these three fields will appear.
  3. Final Answer:

    'title', 'author', and 'published_date' fields -> Option A
  4. Quick Check:

    list_display shows specified fields = A [OK]
Hint: list_display shows only fields listed in its tuple [OK]
Common Mistakes:
  • Assuming all model fields show by default
  • Thinking list_display must include all fields
  • Believing list_display is optional for showing fields
4. What is wrong with this ModelAdmin code snippet?
class AuthorAdmin(admin.ModelAdmin):
    search_fields = 'name'

admin.site.register(Author, AuthorAdmin)
medium
A. search_fields should be a list or tuple, not a string
B. AuthorAdmin must inherit from admin.ModelAdminBase
C. admin.site.register is missing parentheses
D. search_fields cannot be used in ModelAdmin

Solution

  1. Step 1: Check search_fields type

    The search_fields attribute must be a list or tuple of field names, not a single string.
  2. Step 2: Validate other parts

    Inheritance from admin.ModelAdmin is correct, registration syntax is correct, and search_fields is valid in ModelAdmin.
  3. Final Answer:

    search_fields should be a list or tuple, not a string -> Option A
  4. Quick Check:

    search_fields needs list/tuple = C [OK]
Hint: Wrap search_fields in [] or () even for one field [OK]
Common Mistakes:
  • Using a plain string instead of list/tuple for search_fields
  • Confusing ModelAdmin inheritance class
  • Missing parentheses in admin.site.register (not here though)
5. You want to add a filter sidebar in Django admin to filter books by their 'genre' and 'published_year'. Which ModelAdmin attribute should you customize, and how?
hard
A. Add filter_fields = ['genre', 'published_year'] in the ModelAdmin
B. Use search_fields = ('genre', 'published_year') to enable filtering
C. Override filter_sidebar = True in the ModelAdmin
D. Set list_filter = ('genre', 'published_year') in the ModelAdmin class

Solution

  1. Step 1: Identify correct attribute for filters

    The list_filter attribute adds filter sidebars for specified fields in the admin list view.
  2. Step 2: Check other options for correctness

    filter_fields and filter_sidebar do not exist; search_fields is for search, not filters.
  3. Final Answer:

    Set list_filter = ('genre', 'published_year') in the ModelAdmin class -> Option D
  4. Quick Check:

    Use list_filter for sidebar filters = B [OK]
Hint: Use list_filter tuple to add filter sidebar fields [OK]
Common Mistakes:
  • Using search_fields instead of list_filter for filters
  • Trying to use non-existent filter_fields attribute
  • Assuming filter_sidebar is a valid option
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