我有
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,
]
在我的管理页面上注册,但书籍未显示在作者的管理页面中。我误解了这将如何运作?我希望能够在管理页面上添加和删除作者的书籍。
答案 0 :(得分:5)
这是你应该在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)
您可能忘记包含“作者管理员”#39;在这一行:
admin.site.register(Author, AuthorAdmin)