Oct 25, 2018 · by Tim Kamanin
Sometimes in Wagtail, we have pages that should only have a single instance in the system. Let's say we have a blog that has an index page driven by
BlogIndexPage model. We create it once and want to prevent editors from creating a second instance of
BlogIndexPage. How do we do that?
Every Wagtail Page model has a handy flag called
is_creatable, by default it's set to
True. So if we want to prevent a certain type of pages to be creatable via the admin interface, we just need to set
True in the page type class.
Thus in our case, with
BlogIndexPage we do this:
from wagtail.core.models import Page class BlogIndexPage(Page): is_creatable = False
And now editors can't add
BlogIndexPage instances anymore.
The thing is, you might want to add
is_creatable = False to many more model classes and it's always hard to keep up with these settings, especially when you want to turn it on for a moment. I came up with the following solution to the problem: I use a global setting to lock/unlock my models:
class BlogIndexPage(Page): is_creatable = settings.WAGTAIL_PAGES_IS_CREATABLE
Now, with a single change in
settings.py I can set models to be creatable or not:
WAGTAIL_PAGES_IS_CREATABLE = True # or False
The even better idea is to drive this setting via Environment variables, so you don't have to re-deploy every time you want to change the setting,
settings.py I set
WAGTAIL_PAGES_IS_CREATABLE = os.environ.get('WAGTAIL_PAGES_IS_CREATABLE', False)
Voila! Enjoy Wagtail CMS, it's really cool!
Hey, if you've found this useful, please share the post to help other folks find it: