Brylie Christopher Oxley
Brylie Christopher Oxley

Reputation: 1862

How to specify `ordering` for Wagtal PageListingViewSet

I'm creating a PageListingViewSet and would like to specify the default ordering in the table view. Previously, the ModelAdmin class (from which I'm migrating our code) allowed me to specify ordering. Likewise, the Wagtail ModelViewSet allows developers to specify ordering. However, the PageListingViewset doesn't define or inherit from a class that allows ordering to be defined.

I have read the ViewSet docs, reviewed the Wagtail source code, and tried setting ordering on a PageListingViewSet, but can't figure out how to specify the ordering.

How can developers specify, possibly multi-tiered, ordering for Wagtail PageListingViewSets in a conventional manner (i.e., following the Django ordering convention).

Upvotes: 0

Views: 120

Answers (1)

tombreit
tombreit

Reputation: 1349

You can pass a custom subclass of wagtails IndexView to your PageListingViewSet with your default_ordering argument:

In this example I have an EventPage(Page) model with a field date_start and the PageListingViewSet should be initially sorted by that field (instead of the default "-latest_revision_created_at").

# file: wagtail_hooks.py

from wagtail.admin.views.pages.listing import IndexView
from wagtail.admin.viewsets.pages import PageListingViewSet

class EventPageIndexView(IndexView):
    default_ordering = "-date_start"

class EventPageListingViewSet(PageListingViewSet):
    index_view_class = EventPageIndexView

References:

Upvotes: 0

Related Questions