Registering the models in the administration site

Let's add the course models to the administration site. Edit the admin.py file inside the courses application directory and add the following code to it:

from django.contrib import admin
from .models import Subject, Course, Module

@admin.register(Subject)
class SubjectAdmin(admin.ModelAdmin):
list_display = ['title', 'slug']
prepopulated_fields = {'slug': ('title',)}

class ModuleInline(admin.StackedInline):
model = Module

@admin.register(Course)
class CourseAdmin(admin.ModelAdmin):
list_display = ['title', 'subject', 'created']
list_filter = ['created', 'subject']
search_fields = ['title', 'overview']
prepopulated_fields = {'slug': ('title',)}
inlines = [ModuleInline]

The models for the course application are now registered in the administration site. Remember, we use the @admin.register() decorator to register models in the administration site.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.134.79.121