Search code examples
djangodjango-modelsdjango-admin

InlineModelAdmin not showing up on admin page


I have

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,
        ]

registered on my admin page, however books are not showing up in the author's admin page. Am I misunderstanding how this will work? I want to be able to add and remove books from the author on the admin page.


Solution

  • This is what you should have in 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)
    

    You probably forgot to include 'AuthorAdmin' in this line:

    admin.site.register(Author, AuthorAdmin)