1

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.

Dixan
  • 41
  • 6
  • You shouldn't have to include the `inlines` attribute on the ModelAdmin attribute. It should already work without itn as you specified the panel in the models. What error is it throwing exactly? – Alexander Schillemans Nov 26 '22 at 13:03
  • Yeah the `inlines` line I only added after error to try get working. I have removed it as it didn't help(when it wasn't working I was copying my vanilla Django setup to try get working). The error says `KeyError at /admin/books/book/create/` and under that line is `book_review`. So `KeyError` on the inline model I'm trying to add. – Dixan Nov 26 '22 at 16:31

1 Answers1

0

Try changing your Book model to inherit from ClusterableModel (which itself inherits from models.Model)

from modelcluster.models import ClusterableModel

class Book(ClusterableModel):
cnk
  • 981
  • 1
  • 5
  • 9