Dan Swain
Dan Swain

Reputation: 3108

How to choose a Wagtail image across a ParentalManyToManyField?

Environment:

Wagtail 1.13

Django Version: 1.11.11

Python Version: 2.7.12

I'm following the Wagtail documentation to try to add an image showcase (not a category) to a page using ParentalManyToManyField:

class HomePage(Page):
    showcase_title = models.CharField(max_length=100, blank="true", default="SHOWCASE")
    showcase_images = ParentalManyToManyField('wagtailimages.Image')

    content_panels = Page.content_panels + [
        FieldPanel('showcase_title'),
        InlinePanel('showcase_images', label="Showcase images", panels=[
            ImageChooserPanel('showcase_images')
        ]),
    ]

Everything is fine if I comment out the showcase_images editing panel, but I get a KeyError as soon as uncomment the panel showcase_images. In addition to the above variation of editing showcase_images, I've also tried simply InlinePanel('showcase_images'), FieldPanel('showcase_images'), InlinePanel('showcase_images', label="Showcase images", panels=[ImageChooserPanel('image')]), and probably another variation or two. Can someone propose a solution?

Environment:


Request Method: GET
Request URL: http://dev.somedomain.com:8181/admin/pages/3/edit/

Django Version: 1.11.11
Python Version: 2.7.12
Installed Applications:
[u'my_app',
 u'search',
 u'wagtail.wagtailforms',
 u'wagtail.wagtailredirects',
 u'wagtail.wagtailembeds',
 u'wagtail.wagtailsites',
 u'wagtail.wagtailusers',
 u'wagtail.wagtailsnippets',
 u'wagtail.wagtaildocs',
 u'wagtail.wagtailimages',
 u'wagtail.wagtailsearch',
 u'wagtail.wagtailadmin',
 u'wagtail.wagtailcore',
 u'wagtail.contrib.wagtailstyleguide',
 u'modelcluster',
 u'taggit',
 u'wagtailfontawesome',
 u'django.contrib.admin',
 u'django.contrib.auth',
 u'django.contrib.contenttypes',
 u'django.contrib.sessions',
 u'django.contrib.messages',
 u'django.contrib.staticfiles']
Installed Middleware:
[u'django.contrib.sessions.middleware.SessionMiddleware',
 u'django.middleware.common.CommonMiddleware',
 u'django.middleware.csrf.CsrfViewMiddleware',
 u'django.contrib.auth.middleware.AuthenticationMiddleware',
 u'django.contrib.messages.middleware.MessageMiddleware',
 u'django.middleware.clickjacking.XFrameOptionsMiddleware',
 u'django.middleware.security.SecurityMiddleware',
 u'wagtail.wagtailcore.middleware.SiteMiddleware',
 u'wagtail.wagtailredirects.middleware.RedirectMiddleware']



Traceback:

File "/opt/virtualenvs/gmmiTMID/lib/python2.7/site-packages/django/core/handlers/exception.py" in inner
  41.             response = get_response(request)

File "/opt/virtualenvs/gmmiTMID/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "/opt/virtualenvs/gmmiTMID/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/opt/virtualenvs/gmmiTMID/lib/python2.7/site-packages/django/views/decorators/cache.py" in _cache_controlled
  43.             response = viewfunc(request, *args, **kw)

File "/opt/virtualenvs/gmmiTMID/lib/python2.7/site-packages/wagtail/wagtailadmin/urls/__init__.py" in wrapper
  96.             return view_func(request, *args, **kwargs)

File "/opt/virtualenvs/gmmiTMID/lib/python2.7/site-packages/wagtail/wagtailadmin/decorators.py" in decorated_view
  31.             return view_func(request, *args, **kwargs)

File "/opt/virtualenvs/gmmiTMID/lib/python2.7/site-packages/wagtail/wagtailadmin/views/pages.py" in edit
  481.         edit_handler = edit_handler_class(instance=page, form=form)

File "/opt/virtualenvs/gmmiTMID/lib/python2.7/site-packages/wagtail/wagtailadmin/edit_handlers.py" in __init__
  269.             self.children.append(child(instance=self.instance, form=self.form))

File "/opt/virtualenvs/gmmiTMID/lib/python2.7/site-packages/wagtail/wagtailadmin/edit_handlers.py" in __init__
  269.             self.children.append(child(instance=self.instance, form=self.form))

File "/opt/virtualenvs/gmmiTMID/lib/python2.7/site-packages/wagtail/wagtailadmin/edit_handlers.py" in __init__
  693.         self.formset = form.formsets[self.__class__.relation_name]

Exception Type: KeyError at /admin/pages/3/edit/
Exception Value: u'showcase_images'

Upvotes: 0

Views: 687

Answers (1)

gasman
gasman

Reputation: 25237

InlinePanel doesn't work with ParentalManyToManyField relations, only ParentalKey relations. You'll need to set up an intermediate model to define the relation between pages and images, like BlogPageGalleryImage in the tutorial's 'images' section (but with the caption field omitted in this case, so it's just a direct association between pages and images).

(Alternatively, you can use a plain FieldPanel with ParentalManyToManyField, but this will just give you a set of checkboxes for all images in the system, rather than the the image chooser interface.)

Upvotes: 2

Related Questions