Adding read only properties to wagtail page editor

1,046 views
Skip to first unread message

David Burke

unread,
Feb 25, 2015, 5:24:38 PM2/25/15
to wag...@googlegroups.com
Is there any way to add field in the wagtail page editor - like in django admin? 


Use case - Most of my page is editable in Wagtail's page editor - but some parts are too complex like a related inline object within a related inline object.

Matthew Westcott

unread,
Feb 27, 2015, 6:01:16 AM2/27/15
to wag...@googlegroups.com
Hi David,

This isn't something that we've designed for, but I think it should be possible with a bit of digging into Wagtail internals. You'll need to define a custom subclass of wagtail.wagtailadmin.edit_handlers.EditHandler which renders the HTML you want, and reference that from your model's panel definitions. It would be something like the following (untested):


from wagtail.wagtailadmin.edit_handlers import EditHandler
from django.utils.html import format_html

class BaseDisplayFooPropertyPanel(EditHandler):
def __init__(self, instance=None, form=None):
self.instance = instance

def render(self):
return format_html('<p>current value of foo: {0}</p>', self.instance.foo)

def DisplayFooPropertyPanel(some_custom_option):
# create a custom subclass of BaseDisplayFooPropertyPanel -
# this allows us to pass in any parameters we need from the panel definition
# so that we can refer to self.some_custom_option within BaseDisplayFooPropertyPanel
return type(str('_ DisplayFooPropertyPanel'), (BaseDisplayFooPropertyPanel,), {
'some_custom_option': some_custom_option
})


You would then include DisplayFooPropertyPanel(...) in your panel definitions. (Note that this API is changing in Wagtail 0.9 - DisplayFooPropertyPanel would become a class of its own, rather than a function that looks like one. http://docs.wagtail.io/en/latest/releases/0.9.html#edithandler-internal-api-has-changed )


Cheers,
- Matt
Reply all
Reply to author
Forward
0 new messages