InlineModelAdmin 未显示在管理页面上

InlineModelAdmin not showing up on admin page

我有

from django.db import models
from django.contrib import admin

# Create your models here.


class Author(models.Model):
    name = models.CharField(max_length=10)

class Book(models.Model):
    author = models.ForeignKey(Author, null=True, blank=True)
    title = models.CharField(max_length=10)

class BookInline(admin.TabularInline):
    model = Book

class AuthorAdmin(admin.ModelAdmin):
    inlines = [
            BookInline,
        ]

在我的管理页面上注册了,但是书籍没有出现在作者的管理页面上。我误解了这将如何工作吗?我希望能够在管理页面上添加和删除作者的书籍。

这是你应该在 admins.py:

from django.contrib import admin
from models import Author, Book

class BookInline(admin.StackedInline):
    model = Book


class AuthorAdmin(admin.ModelAdmin):
    inlines = [ BookInline ]

admin.site.register(Author, AuthorAdmin)
admin.site.register(Book)

您可能忘记在这一行中包含 'AuthorAdmin':

admin.site.register(Author, AuthorAdmin)