Search code examples
pythondjangowagtail

Unknown field(s) specified for a Page model in Wagtail at content_panels


Wagtail Django

class AboutPage(Page):
    header_image = ImageChooserBlock()
    body = blocks.StreamBlock([
        ('title', blocks.CharBlock()),
        ('content', blocks.RichTextBlock()),
    ])

    content_panels = Page.content_panels + [
        FieldPanel('header_image'),
        FieldPanel('body'),
    ]

I keep getting this error, and I can't even begin to understand how to debug

Exception in thread django-main-thread:
Traceback (most recent call last):
  File "/usr/lib/python3.10/threading.py", line 1016, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.10/threading.py", line 953, in run
    self._target(*self._args, **self._kwargs)
  File "/home/khophi/Development/Photograph/venv/lib/python3.10/site-packages/django/utils/autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "/home/khophi/Development/Photograph/venv/lib/python3.10/site-packages/django/core/management/commands/runserver.py", line 133, in inner_run
    self.check(display_num_errors=True)
  File "/home/khophi/Development/Photograph/venv/lib/python3.10/site-packages/django/core/management/base.py", line 485, in check
    all_issues = checks.run_checks(
  File "/home/khophi/Development/Photograph/venv/lib/python3.10/site-packages/django/core/checks/registry.py", line 88, in run_checks
    new_errors = check(app_configs=app_configs, databases=databases)
  File "/home/khophi/Development/Photograph/venv/lib/python3.10/site-packages/wagtail/admin/checks.py", line 70, in get_form_class_check
    if not issubclass(edit_handler.get_form_class(), WagtailAdminPageForm):
  File "/home/khophi/Development/Photograph/venv/lib/python3.10/site-packages/wagtail/admin/panels/base.py", line 134, in get_form_class
    return get_form_for_model(
  File "/home/khophi/Development/Photograph/venv/lib/python3.10/site-packages/wagtail/admin/panels/base.py", line 48, in get_form_for_model
    return metaclass(class_name, tuple(bases), form_class_attrs)
  File "/home/khophi/Development/Photograph/venv/lib/python3.10/site-packages/permissionedforms/forms.py", line 30, in __new__
    new_class = super().__new__(mcs, name, bases, attrs)
  File "/home/khophi/Development/Photograph/venv/lib/python3.10/site-packages/modelcluster/forms.py", line 259, in __new__
    new_class = super().__new__(cls, name, bases, attrs)
  File "/home/khophi/Development/Photograph/venv/lib/python3.10/site-packages/django/forms/models.py", line 321, in __new__
    raise FieldError(message)
django.core.exceptions.FieldError: Unknown field(s) (body, header_image) specified for AboutPage
9:43

Anyone with Wagtail Django experience should help


Solution

  • Blocks such as ImageChooserBlock and StreamBlock are only usable inside StreamField - they are not the same thing as model fields, and using them where a model field definition is expected will not work.

    The correct equivalents in this case would be a foreign key to the Image model, and a StreamField:

    from django.db import models
    from wagtail.fields import StreamField
    
    class AboutPage(Page):
        header_image = models.ForeignKey(
            "wagtailimages.Image", null=True, blank=True,
            on_delete=models.SET_NULL, related_name="+"
        )
        body = StreamField([
            ('title', blocks.CharBlock()),
            ('content', blocks.RichTextBlock()),
        ])
    
        content_panels = Page.content_panels + [
            FieldPanel('header_image'),
            FieldPanel('body'),
        ]