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?
from django.contrib import admin from .models import Book admin.site.register(Book())
Remember that admin.site.register() expects a model class, not an instance.
The admin.site.register() function requires the model class itself, not an instance. Passing Book() creates an instance, which causes a TypeError.
You want to register two models, Author and Publisher, in the Django admin. Which of the following code snippets is correct?
Think about how to register multiple models separately or together.
Each model must be registered individually or passed as a list. Option C registers each model separately, which is correct. Option C is close but Django expects a list, not a tuple or set. Option C passes two arguments incorrectly. Option C uses a set which is invalid.
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?
from django.contrib import admin from .models import Article class ArticleAdmin(admin.ModelAdmin): list_display = ('title', 'published_date') admin.site.register(Article)
Check how the admin class is linked to the model registration.
The custom admin class must be passed as the second argument to admin.site.register() to apply its settings. Without it, the default admin is used.
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?
from django.contrib import admin from .models import Event @admin.register(Event) class EventAdmin(admin.ModelAdmin): list_display = ('name', 'date')
Think about what the @admin.register() decorator does.
The decorator registers the model with the specified admin class, applying its configuration such as list_display.
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?
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)
Think about how Django admin handles multiple registrations of the same model.
Django admin raises an AlreadyRegistered exception if you try to register the same model more than once without unregistering it first.