0
0
Djangoframework~20 mins

Registering models in admin in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Django Admin Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when accessing the Django admin after registering a model incorrectly?

Consider the following Django admin registration code:

from django.contrib import admin
from .models import Book

admin.site.register(Book())

What happens when you try to access the Django admin page for the Book model?

Django
from django.contrib import admin
from .models import Book

admin.site.register(Book())
ARaises a TypeError because an instance is passed instead of the model class.
BThe admin page shows the Book model list correctly.
CThe admin page shows but with no data listed.
DRaises an ImportError due to wrong import syntax.
Attempts:
2 left
💡 Hint

Remember that admin.site.register() expects a model class, not an instance.

📝 Syntax
intermediate
2:00remaining
Which code correctly registers multiple models in Django admin?

You want to register two models, Author and Publisher, in the Django admin. Which of the following code snippets is correct?

Aadmin.site.register(Author, Publisher)
Badmin.site.register({Author, Publisher})
Cadmin.site.register(Author); admin.site.register(Publisher)
Dadmin.site.register([Author, Publisher])
Attempts:
2 left
💡 Hint

Think about how to register multiple models separately or together.

🔧 Debug
advanced
2:00remaining
Why does this custom admin class not apply to the model in Django admin?

Given this code:

from django.contrib import admin
from .models import Article

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

admin.site.register(Article)

Why does the admin page not show the custom list display?

Django
from django.contrib import admin
from .models import Article

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

admin.site.register(Article)
ABecause admin.site.register requires a decorator.
BBecause list_display is not a valid attribute for ModelAdmin.
CBecause the Article model is not imported correctly.
DBecause the admin class ArticleAdmin was not passed to admin.site.register.
Attempts:
2 left
💡 Hint

Check how the admin class is linked to the model registration.

state_output
advanced
2:00remaining
What is the effect of this admin registration with a decorator?

Consider this Django admin code:

from django.contrib import admin
from .models import Event

@admin.register(Event)
class EventAdmin(admin.ModelAdmin):
    list_display = ('name', 'date')

What happens when you access the admin page for Event?

Django
from django.contrib import admin
from .models import Event

@admin.register(Event)
class EventAdmin(admin.ModelAdmin):
    list_display = ('name', 'date')
AThe Event model is registered with the custom EventAdmin showing name and date columns.
BThe Event model is registered but uses default admin settings ignoring list_display.
CRaises a SyntaxError because decorators cannot be used with admin classes.
DThe Event model is not registered at all.
Attempts:
2 left
💡 Hint

Think about what the @admin.register() decorator does.

🧠 Conceptual
expert
2:00remaining
What error occurs if you register the same model twice with different admin classes?

Given this code snippet:

from django.contrib import admin
from .models import Product

class ProductAdmin1(admin.ModelAdmin):
    list_display = ('name',)

class ProductAdmin2(admin.ModelAdmin):
    list_display = ('price',)

admin.site.register(Product, ProductAdmin1)
admin.site.register(Product, ProductAdmin2)

What happens when Django starts?

Django
from django.contrib import admin
from .models import Product

class ProductAdmin1(admin.ModelAdmin):
    list_display = ('name',)

class ProductAdmin2(admin.ModelAdmin):
    list_display = ('price',)

admin.site.register(Product, ProductAdmin1)
admin.site.register(Product, ProductAdmin2)
ARaises a SyntaxError due to duplicate registration.
BRaises django.contrib.admin.sites.AlreadyRegistered error.
CBoth admin classes are merged and applied.
DThe second registration overrides the first without error.
Attempts:
2 left
💡 Hint

Think about how Django admin handles multiple registrations of the same model.