【问题标题】:Wagtail Inlinepanel demo erorrWagtail 内联面板演示错误
【发布时间】:2017-02-02 21:00:47
【问题描述】:

我对 Django Wagtail 比较陌生,我正在关注 docs.wagtail.io 网站上的演示,该网站可以在 here 找到,了解如何使用带有相关链接的 InlinePanel 将链接列表添加到页面 我似乎遇到了一个我不完全理解它的含义的错误。 错误说

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

演示代码如下

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

我的主要目标是学习这一点,这样我就可以将图像链接添加到我正在开发的 BlogPage 应用程序的侧边栏。

【问题讨论】:

  • 您安装了哪些版本的 Django、Wagtail 和 django-modelcluster 包? (您可以通过运行pip freeze 并查找相关行来查找)
  • 版本 1.8.1 ,我用pip install wagtail 得到它,我以为它是最新的
  • 好的,这就是 Wagtail 的版本 - Django 和 django-modelcluster 呢?
  • Django==1.10.5 django-modelcluster==2.0

标签: wagtail


【解决方案1】:

您的 InlinePanel 声明不太正确 - 它必须是:

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

这是怎么回事:

  • 通过使用related_name='related_links' 定义ParentalKey,您可以在BookPage 上建立一个名为related_links 的一对多关系。这允许您检索与给定 BookPage 实例关联的所有 BookPageRelatedLinks 对象(例如,如果您的 BookPage 实例被称为 page,则可以写为 page.related_links.all())。
  • InlinePanel 声明然后告诉 Wagtail 使 related_links 属性在管理员中可编辑。

您收到误导性错误消息的原因是您在 BookPage 中定义了 RelatedLinkBookPageRelatedLinks 类 - 这有点不寻常,但仍然有效。这导致 BookPageRelatedLinks 被定义为 BookPage 的属性(即 BookPage.BookPageRelatedLinks)。然后,当 Wagtail 尝试设置 InlinePanel 时,它会检索该属性并失败,因为它不是预期的对象类型(它是类定义,而不是关系)。

如果您以更传统的方式编写模型文件,相关模型定义在下方(或上方)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')

...那么你会得到一个信息更丰富的错误:AttributeError: type object 'BookPage' has no attribute 'BookPageRelatedLinks'

【讨论】:

  • 以及当错误是那个错误时,我该如何在这种情况下找到解决方案,因为该错误是我在开始调整代码并在 BookPage 类中定义相关链接类之前遇到的第一个错误
  • 同样,修复方法是使用InlinePanel('related_links', label="Related Links")
猜你喜欢
  • 2015-09-11
  • 2017-05-24
  • 1970-01-01
  • 2018-10-04
  • 2019-06-16
  • 2012-06-18
  • 2017-11-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多