Search History: Adding Page Privacy in Wagtail Programmatically


  • Wagtail 2.1.1
  • Django 1.11
  • Python 3.6

This week I needed to manipulate the privacy of a Wagtail page programmatically. The Wagtail docs show you how to edit page privacy in the admin UI, but it doesn't peek behind the curtain to show you the code. The example here is a result of my Google searching.


For this example, assume we have a group called "members," and we need to restrict a page to be seen only by members of that group. This code assumes you have a page instance from a model that inherits from Wagtail's Page model, and that you have already defined your group.

from django.contrib.auth.models import Group
from wagtail.core.models import PageViewRestriction
from app.models import MyPage

members_group = Group.objects.get(name='members')
my_page = MyPage.objects.get(slug='my-slug')

Once you have retrieved the group and the page you want to restrict, create a new PageViewRestriction object. Because the groups attribute on the PageViewRestriction model is a ManyToMany field, you must create the restriction instance before you add the group.

restriction = PageViewRestriction.objects.create(

Now add the group that is allowed to see the page.


See the code for the PageViewRestriction class, and the available RESTRICTION_CHOICES (where I got PageViewRestriction.GROUPS) in the BaseViewRestriction class.

Thanks to Jeff Triplett for proofreading a draft of this article.