Ayabonga Qwabi
Ayabonga Qwabi

Reputation: 316

Wagtail Inlinepanel demo erorr

I am relatively new to Django Wagtail and I was following the demo from the docs.wagtail.io website which can be found here on how to add a list of Links to a Page using an InlinePanel with Related links I seem to have reached an error that I donot fully understand it's meaning. The error says

AttributeError: type object 'BookPageRelatedLinks' has no attribute 'rel'

The code for the demo is as follows

from wagtail.wagtailcore.models import Orderable, Page
from modelcluster.fields import ParentalKey
from wagtail.wagtailadmin.edit_handlers import FieldPanel,InlinePanel
from django.db import models



class BookPage(Page):
  # The abstract model for related links, complete with panels
  class RelatedLink(models.Model):
      title = models.CharField(max_length=255)
      link_external = models.URLField("External link", blank=True)

      panels = [
          FieldPanel('title'),
          FieldPanel('link_external'),
      ]

      class Meta:
          abstract = True

  # The real model which combines the abstract model, an
  # Orderable helper class, and what amounts to a ForeignKey link
  # to the model we want to add related links to (BookPage)
  class BookPageRelatedLinks(Orderable, RelatedLink):
      page = ParentalKey('demo.BookPage', related_name='related_links')

  content_panels = Page.content_panels + [
    InlinePanel('BookPageRelatedLinks', label="Related Links"),
  ]

My primary objective was to learn this so I can add image links to a sidebar on a BlogPage app I am developing.

Upvotes: 0

Views: 913

Answers (1)

gasman
gasman

Reputation: 25292

Your InlinePanel declaration isn't quite correct - it needs to be:

InlinePanel('related_links', label="Related Links")

Here's what's going on:

  • By defining a ParentalKey with related_name='related_links', you set up a one-to-many relation called related_links on BookPage. This allows you to retrieve all of the BookPageRelatedLinks objects associated with a given BookPage instance (for example, if your BookPage instance was called page, you could write page.related_links.all()).
  • The InlinePanel declaration then tells Wagtail to make the related_links property editable within the admin.

The reason you're getting a misleading error message is that you've defined the RelatedLink and BookPageRelatedLinks classes inside the BookPage - which is a little bit unusual, but still valid. This results in BookPageRelatedLinks being defined as a property of BookPage (i.e. BookPage.BookPageRelatedLinks). Then, when Wagtail tries to set up the InlinePanel, it retrieves that property and fails because it's not the expected type of object (it's a class definition, not a relation).

If you write your models file in the more conventional way, with the related models defined below (or above) BookPage:

class BookPage(Page):
    content_panels = Page.content_panels + [
        InlinePanel('BookPageRelatedLinks', label="Related Links"),
    ]


# The abstract model for related links, complete with panels
class RelatedLink(models.Model):
    title = models.CharField(max_length=255)
    link_external = models.URLField("External link", blank=True)

    panels = [
        FieldPanel('title'),
        FieldPanel('link_external'),
    ]

    class Meta:
        abstract = True


# The real model which combines the abstract model, an
# Orderable helper class, and what amounts to a ForeignKey link
# to the model we want to add related links to (BookPage)
class BookPageRelatedLinks(Orderable, RelatedLink):
    page = ParentalKey('home.BookPage', related_name='related_links')

...then you would get a more informative error: AttributeError: type object 'BookPage' has no attribute 'BookPageRelatedLinks'.

Upvotes: 2

Related Questions