Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 237
butteredpopcorn Avatar asked Nov 22 '25 05:11

butteredpopcorn


1 Answers

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)
like image 103
Cheng Avatar answered Nov 25 '25 00:11

Cheng



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!