Move SnippetViewSet.list_per_page to ModelViewSet

pull/10822/head
Sage Abdullah 2023-08-23 16:13:03 +01:00
rodzic 66216852b8
commit 9a2d53d0cf
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: EB1A33CC51CC0217
6 zmienionych plików z 37 dodań i 6 usunięć

Wyświetl plik

@ -80,6 +80,7 @@ Viewsets are Wagtail's mechanism for defining a group of related admin views wit
:attr:`~django.db.models.Options.verbose_name_plural`.
.. autoattribute:: add_to_reference_index
.. autoattribute:: list_per_page
.. autoattribute:: list_display
.. autoattribute:: list_export
.. autoattribute:: list_filter
@ -152,7 +153,6 @@ Viewsets are Wagtail's mechanism for defining a group of related admin views wit
.. autoclass:: wagtail.snippets.views.snippets.SnippetViewSet
.. autoattribute:: model
.. autoattribute:: list_per_page
.. autoattribute:: chooser_per_page
.. autoattribute:: ordering
.. autoattribute:: inspect_view_enabled

Wyświetl plik

@ -86,7 +86,7 @@ Similar URL customisations are also possible for the snippet chooser views throu
## Listing view
The {attr}`~wagtail.admin.viewsets.model.ModelViewSet.list_display` attribute can be set to specify the columns shown on the listing view. To customise the number of items to be displayed per page, you can set the {attr}`~wagtail.snippets.views.snippets.SnippetViewSet.list_per_page` attribute (or {attr}`~wagtail.snippets.views.snippets.SnippetViewSet.chooser_per_page` for the chooser listing).
The {attr}`~wagtail.admin.viewsets.model.ModelViewSet.list_display` attribute can be set to specify the columns shown on the listing view. To customise the number of items to be displayed per page, you can set the {attr}`~wagtail.admin.viewsets.model.ModelViewSet.list_per_page` attribute (or {attr}`~wagtail.snippets.views.snippets.SnippetViewSet.chooser_per_page` for the chooser listing).
To customise the base queryset for the listing view, you could override the {meth}`~wagtail.snippets.views.snippets.SnippetViewSet.get_queryset` method. Additionally, the {attr}`~wagtail.snippets.views.snippets.SnippetViewSet.ordering` attribute can be used to specify the default ordering of the listing view.

Wyświetl plik

@ -546,3 +546,33 @@ class TestListExport(WagtailTestUtils, TestCase):
self.assertEqual(cell_array[1], ["Lotso", datetime.date(2010, 6, 18), "False"])
self.assertEqual(cell_array[2], ["level", datetime.date(2010, 6, 18), "True"])
self.assertEqual(len(cell_array), 3)
class TestPagination(WagtailTestUtils, TestCase):
def setUp(self):
self.user = self.login()
@classmethod
def setUpTestData(cls):
objects = [FeatureCompleteToy(name=f"Frisbee {i}") for i in range(32)]
FeatureCompleteToy.objects.bulk_create(objects)
def test_default_list_pagination(self):
list_url = reverse("fctoy_alt1:index")
response = self.client.get(list_url)
# Default is 20 per page
self.assertEqual(FeatureCompleteToy.objects.all().count(), 32)
self.assertContains(response, "Page 1 of 2")
self.assertContains(response, "Next")
self.assertContains(response, list_url + "?p=2")
def test_custom_list_pagination(self):
list_url = reverse("feature_complete_toy:index")
response = self.client.get(list_url)
# Custom is set to display 5 per page
self.assertEqual(FeatureCompleteToy.objects.all().count(), 32)
self.assertContains(response, "Page 1 of 7")
self.assertContains(response, "Next")
self.assertContains(response, list_url + "?p=2")

Wyświetl plik

@ -41,6 +41,9 @@ class ModelViewSet(ViewSet):
#: The prefix of template names to look for when rendering the admin views.
template_prefix = ""
#: The number of items to display per page in the index view. Defaults to 20.
list_per_page = 20
def __init__(self, name=None, **kwargs):
super().__init__(name=name, **kwargs)
if not self.model:
@ -83,6 +86,7 @@ class ModelViewSet(ViewSet):
"filterset_class": self.filterset_class,
"search_fields": self.search_fields,
"search_backend_name": self.search_backend_name,
"paginate_by": self.list_per_page,
**kwargs,
}

Wyświetl plik

@ -609,9 +609,6 @@ class SnippetViewSet(ModelViewSet):
#: The model class to be registered as a snippet with this viewset.
model = None
#: The number of items to display per page in the index view. Defaults to 20.
list_per_page = 20
#: The number of items to display in the chooser view. Defaults to 10.
chooser_per_page = 10
@ -791,7 +788,6 @@ class SnippetViewSet(ModelViewSet):
index_url_name=self.get_url_name("list"),
index_results_url_name=self.get_url_name("list_results"),
delete_url_name=self.get_url_name("delete"),
paginate_by=self.list_per_page,
default_ordering=self.ordering,
**kwargs,
)

Wyświetl plik

@ -208,6 +208,7 @@ class FeatureCompleteToyViewSet(ModelViewSet):
list_filter = ["name", "release_date"]
list_export = ["name", "release_date", "is_cool"]
export_filename = "feature-complete-toys"
list_per_page = 5
# search_fields derived from the model