Customizing admin views for snippets¶
Additional customizations to the admin views for each snippet model can be achieved through a custom SnippetViewSet
class. The SnippetViewSet
is a subclass of ModelViewSet
, with snippets-specific properties provided by default. Hence, it supports the same customizations provided by ModelViewSet
such as customizing the listing view (e.g. adding custom columns, and filters), creating a custom menu item, and more.
Before proceeding, ensure that you register the snippet model using register_snippet
as a function instead of a decorator, as described in Registering snippets.
For demonstration, consider the following Member
model and a MemberFilterSet
class:
# models.py
from django.db import models
from wagtail.admin.filters import WagtailFilterSet
class Member(models.Model):
class ShirtSize(models.TextChoices):
SMALL = "S", "Small"
MEDIUM = "M", "Medium"
LARGE = "L", "Large"
EXTRA_LARGE = "XL", "Extra Large"
name = models.CharField(max_length=255)
shirt_size = models.CharField(max_length=5, choices=ShirtSize.choices, default=ShirtSize.MEDIUM)
def get_shirt_size_display(self):
return self.ShirtSize(self.shirt_size).label
get_shirt_size_display.admin_order_field = "shirt_size"
get_shirt_size_display.short_description = "Size description"
class MemberFilterSet(WagtailFilterSet):
class Meta:
model = Member
fields = ["shirt_size"]
And the following is the snippet’s corresponding SnippetViewSet
subclass:
from wagtail.admin.panels import FieldPanel
from wagtail.admin.ui.tables import UpdatedAtColumn
from wagtail.snippets.models import register_snippet
from wagtail.snippets.views.snippets import SnippetViewSet
from myapp.models import Member, MemberFilterSet
class MemberViewSet(SnippetViewSet):
model = Member
icon = "user"
list_display = ["name", "shirt_size", "get_shirt_size_display", UpdatedAtColumn()]
list_per_page = 50
copy_view_enabled = False
inspect_view_enabled = True
admin_url_namespace = "member_views"
base_url_path = "internal/member"
filterset_class = MemberFilterSet
# alternatively, you can use the following instead of filterset_class
# list_filter = ["shirt_size"]
# or
# list_filter = {"shirt_size": ["exact"], "name": ["icontains"]}
edit_handler = TabbedInterface([
ObjectList([FieldPanel("name")], heading="Details"),
ObjectList([FieldPanel("shirt_size")], heading="Preferences"),
])
register_snippet(MemberViewSet)
Icon¶
You can define an icon
attribute on the SnippetViewSet
to specify the icon that is used across the admin for this snippet type. The icon
needs to be registered in the Wagtail icon library. If icon
is not set, the default "snippet"
icon is used.
URL namespace and base URL path¶
The url_namespace
property can be overridden to use a custom URL namespace for the URL patterns of the views. If unset, it defaults to wagtailsnippets_{app_label}_{model_name}
. Meanwhile, overriding url_prefix
allows you to customize the base URL path relative to the Wagtail admin URL. If unset, it defaults to snippets/app_label/model_name
.
Similar URL customizations are also possible for the snippet chooser views through chooser_admin_url_namespace
, chooser_base_url_path
, get_chooser_admin_url_namespace()
, and get_chooser_admin_base_path()
.
Listing view¶
You can customize the listing view to add custom columns, filters, pagination, etc. via various attributes available on the SnippetViewSet
. Refer to the listing view customizations for ModelViewSet
for more details.
Additionally, you can customize the base queryset for the listing view by overriding the get_queryset()
method.
Copy view¶
Added in version 6.0.
The copy view is enabled by default and will be accessible by users with the ‘add’ permission on the model. To disable it, set copy_view_enabled
to False
. Refer to the copy view customizations for ModelViewSet
for more details.
Inspect view¶
The inspect view is disabled by default, as it’s not often useful for most models. To enable it, set inspect_view_enabled
to True
. Refer to the inspect view customizations for ModelViewSet
for more details.
Templates¶
Template customizations work the same way as for ModelViewSet
, except that the template_prefix
defaults to wagtailsnippets/snippets/
. Refer to the template customizations for ModelViewSet
for more details.