Junior — Middle
How does the model connect to the admin panel in Django?
sobes.tech AI
Answer from AI
In Django, to connect a model to the admin panel, you need to register it in your application's admin.py file. This is done using the admin.site.register() function.
Example of registering the Book model:
from django.contrib import admin
from .models import Book
admin.site.register(Book)
After this, the Book model will be available in the Django admin panel, where you can create, edit, and delete records.
For more fine-tuned control over how the model appears in the admin, you can create a subclass of admin.ModelAdmin and pass it as a second argument:
class BookAdmin(admin.ModelAdmin):
list_display = ('title', 'author', 'published_date')
admin.site.register(Book, BookAdmin)
This allows you to customize which fields are displayed in the list, filters, search, and other parameters.