Having trouble populating getting ParentalKey fields in control panel

115 views
Skip to first unread message

Chris May

unread,
Mar 8, 2016, 7:19:25 PM3/8/16
to Wagtail support

Like many who post here, I'm new to Wagtail and Django.

I'm trying to learn as much as I can from the documentation, the demo project, and this forum; but it seems there's at least one issue that doesn't quite click for me yet.

I'm trying to rebuild a site in Wagtail for a construction company, and in particular I'm working on their "projects". Each project needs to have at least one photo, but as many as they'd like to upload. I've tried using the Adverts and the Carousels in the demo app as inspiration, but a photo field never shows up, much less an orderable photo field.

Here's my code, such as it is:

class ProjectPhotos(models.Model):
    photo = models.ForeignKey(
        'wagtailimages.Image',
        related_name='+'
    )

    panels = [
        ImageChooserPanel('photo')
    ]


class ProjectPhotoPlacement(Orderable, ProjectPhotos):
    project = ParentalKey('home.Project', related_name='project_photos')


@register_snippet
class Project(Orderable, ClusterableModel):
    location = models.CharField(max_length=255, help_text="City, State")
    footprint = models.CharField(max_length=255, null=True)
    duration = models.CharField(max_length=255, null=True)

    content_panels = [
        InlinePanel('location'),
        FieldPanel('footprint'),
        FieldPanel('duration'),
        InlinePanel('project_photos', label="Project Photos")
    ]

I've tried just about every combination I could think of. Where am I going off track?

Matthew Westcott

unread,
Mar 9, 2016, 10:23:02 AM3/9/16
to wag...@googlegroups.com
Hi Chris,

I don't think you need the split between ProjectPhotos and ProjectPhotoPlacement. The Advert / AdvertPlacement pattern as used on the demo site comes into play when you're defining a new content model and a link table so that you can associate it with pages/snippets in a many-to-many fashion - but in your case, the thing you're attaching to Project is just a plain Image, not a new content type:


class ProjectPhoto(Orderable):
project = ParentalKey('home.Project', related_name='project_photos')
photo = models.ForeignKey(
'wagtailimages.Image',
related_name='+'
)

panels = [
ImageChooserPanel('photo'),
]

@register_snippet
class Project(ClusterableModel):
...
content_panels = [
...
InlinePanel('project_photos', label="Project Photos"),
]


Cheers,
- Matt

Chris May

unread,
Mar 9, 2016, 11:34:06 AM3/9/16
to Wagtail support
Thanks, for reaching out Matt.

I was thinking that was the reason it was split. Glad to know that was the intention.

I make the changes I could find and migrated, but to no avail. I'm still not getting the photos field in the admin area.

Updated code below.
class ProjectPhotos(Orderable):
    project = ParentalKey('home.Project', related_name='project_photos')
    photo = models.ForeignKey(
        'wagtailimages.Image',
        related_name='+'
    )

    panels = [
        ImageChooserPanel('photo')
    ]


@register_snippet
class Project(ClusterableModel):
    location = models.CharField(max_length=255, help_text="City, State")
    footprint = models.CharField(max_length=255, null=True)
    duration = models.CharField(max_length=255, null=True)

    content_panels = [
        InlinePanel('location'),
        FieldPanel('footprint'),
        FieldPanel('duration'),
        InlinePanel('project_photos', label="Project Photos")
    ]

I would not be surprised if it's something stupidly subtle, but I can't see it.

Chris May

unread,
Mar 9, 2016, 11:42:42 AM3/9/16
to Wagtail support
Hmmm... It also seems as though the content_panels attribute (property? variable?) isn't doing anything. 

I can delete any line in it or the whole block, and Location, Footprint, and Duration still show up in the control panel.

Matthew Westcott

unread,
Mar 9, 2016, 11:51:00 AM3/9/16
to wag...@googlegroups.com
Ah yes... for snippets, you should use 'panels' instead of 'content_panels'.

(The name 'content_panels' on page models refers to the fact that they're the panels that appear within the Content tab, as opposed to Promote or Settings - in the snippet editing interface, there are no tabs as standard, so it's just 'panels'. Although to muddy the waters further, Wagtail 1.4 now *does* allow you to customise that for snippets, via the 'edit_handler' property: http://docs.wagtail.io/en/latest/advanced_topics/customisation/page_editing_interface.html#customising-the-tabbed-interface)

Cheers,
- Matt

Chris May

unread,
Mar 9, 2016, 11:58:55 AM3/9/16
to Wagtail support
AH HAH! Of course! That solved it!

Good to know about the customizations! I got so accustomed to the content/promote/settings panels I didn't notice they weren't there in the snippet.

Thanks again, Matt!

Reply all
Reply to author
Forward
0 new messages