I have two custom models(not inheriting from Page
) that are specific to the admin in a Wagtail CMS website. I can get this working in regular Django, but in Wagtail I can't get the inline model fields to appear. I get a key error
. The code...
On model.py
:
from django.db import models
from wagtail.admin.panels import (
FieldPanel,
MultiFieldPanel,
FieldRowPanel,
InlinePanel,
)
from author.models import Author
class Book(models.Model):
author = models.ForeignKey(Author, on_delete=models.CASCADE)
date = models.DateField("Date released")
panels = [
MultiFieldPanel([
InlinePanel('book_review'),
], heading="Book reviews"),
]
class BookReview(models.Model):
book = models.ForeignKey(
Book, on_delete=models.CASCADE, related_name='book_review')
title = models.CharField(max_length=250)
content = models.TextField()
panels = [
FieldRowPanel([
FieldPanel('title'),
FieldPanel('content'),
])
]
and wagtail_hooks.py
:
from wagtail.contrib.modeladmin.options import (
ModelAdmin,
modeladmin_register,
)
from .models import Book, BookReview
class BookAdmin(ModelAdmin):
model = Book
add_to_settings_menu = False
add_to_admin_menu = True
inlines = [BookReview] # only added this after key error, but it didn't help
modeladmin_register(BookAdmin)
How can I get the InlinePanel('book_review'),
line to show up in the admin. It all works until I try add the inline model fields.
I looked around online and it was mentioning a third party Django modelcluster
package. Is this still required? Those post were quite old(5 years or so). Or instead of using ForeignKey
use ParentalKey
, but that's only if inheriting from Page
model.