向页面添加 content_panels 字段时 FieldDoesNotExist

FieldDoesNotExist when adding content_panels fields to Page

我的头撞在墙上很疼...为什么内容面板无法识别我的 DetailPage 中的字段?一切正常,直到我将 FieldPanel 添加到 content_panels。

哦,这也是用2.0版的wagtail。这似乎与字段类型无关,事件是一个简单的 CharBlock 触发此错误。

堆栈跟踪

Performing system checks...

Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x0000005F7CDB5598>
Traceback (most recent call last):
  File "C:\Users\RichardK\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\options.py", line 566, in get_field
    return self.fields_map[field_name]
KeyError: 'links'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\RichardK\AppData\Local\Programs\Python\Python36\lib\site-packages\django\utils\autoreload.py", line 225, in wrapper
    fn(*args, **kwargs)
...
  File "C:\Users\RichardK\AppData\Local\Programs\Python\Python36\lib\site-packages\wagtail\admin\edit_handlers.py", line 766, in get_edit_handler
    return edit_handler.bind_to_model(cls)
  File "C:\Users\RichardK\AppData\Local\Programs\Python\Python36\lib\site-packages\wagtail\admin\edit_handlers.py", line 130, in bind_to_model
    new.on_model_bound()
  File "C:\Users\RichardK\AppData\Local\Programs\Python\Python36\lib\site-packages\wagtail\admin\edit_handlers.py", line 275, in on_model_bound
    for child in self.children]
  File "C:\Users\RichardK\AppData\Local\Programs\Python\Python36\lib\site-packages\wagtail\admin\edit_handlers.py", line 275, in <listcomp>
    for child in self.children]
  File "C:\Users\RichardK\AppData\Local\Programs\Python\Python36\lib\site-packages\wagtail\admin\edit_handlers.py", line 130, in bind_to_model
    new.on_model_bound()
  File "C:\Users\RichardK\AppData\Local\Programs\Python\Python36\lib\site-packages\wagtail\admin\edit_handlers.py", line 275, in on_model_bound
    for child in self.children]
  File "C:\Users\RichardK\AppData\Local\Programs\Python\Python36\lib\site-packages\wagtail\admin\edit_handlers.py", line 275, in <listcomp>
    for child in self.children]
  File "C:\Users\RichardK\AppData\Local\Programs\Python\Python36\lib\site-packages\wagtail\admin\edit_handlers.py", line 130, in bind_to_model
    new.on_model_bound()
  File "C:\Users\RichardK\AppData\Local\Programs\Python\Python36\lib\site-packages\wagtail\admin\edit_handlers.py", line 479, in on_model_bound
    self.db_field = self.model._meta.get_field(self.field_name)
  File "C:\Users\RichardK\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\options.py", line 568, in get_field
    raise FieldDoesNotExist("%s has no field named '%s'" % (self.object_name, field_name))
django.core.exceptions.FieldDoesNotExist: DetailPage has no field named 'links'

models.py 来自 django.db 导入模型

from wagtail.core.models import Page
from wagtail.core.fields import RichTextField
from wagtail.core import blocks
from wagtail.core.blocks import ListBlock, URLBlock, StructBlock, TextBlock, CharBlock
from wagtail.admin.edit_handlers import FieldPanel
from wagtail.search import index


class DetailPage(Page):
    """
    Two column detail page with quick links on the left
    """
    links = blocks.ListBlock(blocks.URLBlock(), null=True, blank=True)
    body = RichTextField(blank=True)

    content_panels = Page.content_panels + [
        FieldPanel('links', classname="full"),
        FieldPanel('body')
    ]

ListBlockCharBlock 等其他块类型不能与 Django's model fields 互换 - 它们不能直接在页面模型定义中使用,只能在 StreamField 中使用。