Repository: wagtail/wagtail

Issue:
Missing media files for filters on snippets listing view
<!--
Found a bug? Please fill out the sections below. 👍
-->

### Issue Summary

When using a custom `filterset_class` on a custom `SnippetViewSet`, if the `FilterSet` class produces an input widget that needs additional JS files e.g. a datetime picker, the JS file won't be included in the HTML. Thus, an error may show up on the client side, e.g. `Uncaught ReferenceError: initDateChooser is not defined`:

<img width="422" alt="image" src="https://user-images.githubusercontent.com/6379424/226655055-a3cdb0a0-83ec-4de2-8f5b-493413be0309.png">

### Steps to Reproduce

1. Spin up bakerydemo
2. Remove `@register_snippet` from the `Person` model in `bakerydemo/base/models.py`
3. Edit `bakerydemo/base/wagtail_hooks.py` to have the following:
    ```py
    from wagtail.admin.filters import WagtailFilterSet
    from wagtail.snippets.views.snippets import SnippetViewSet


    class FilterableSnippetFilterSet(WagtailFilterSet):
        class Meta:
            model = Person
            fields = ["first_published_at"]


    class PersonViewSet(SnippetViewSet):
        filterset_class = FilterableSnippetFilterSet


    register_snippet(Person, viewset=PersonViewSet)
    ```
4. Run the server and open the listing page
5. Click on the filter field for "First published at", the date picker will not show up
6. Upon checking the browser console, `Uncaught ReferenceError: initDateChooser is not defined` can be seen.

Any other relevant information. For example, why do you consider this a bug and what did you expect to happen instead?

The date picker should show up and the error shouldn't exist.


Discussion:

Resolving patch:
diff --git a/wagtail/admin/views/generic/models.py b/wagtail/admin/views/generic/models.py
--- a/wagtail/admin/views/generic/models.py
+++ b/wagtail/admin/views/generic/models.py
@@ -332,6 +332,7 @@ def get_context_data(self, *args, object_list=None, **kwargs):
 
         index_url = self.get_index_url()
         table = self.get_table(context["object_list"], base_url=index_url)
+        context["media"] = table.media
 
         context["can_add"] = (
             self.permission_policy is None
@@ -346,9 +347,9 @@ def get_context_data(self, *args, object_list=None, **kwargs):
             context["is_filtering"] = any(
                 self.request.GET.get(f) for f in set(self.filters.get_fields())
             )
+            context["media"] += self.filters.form.media
 
         context["table"] = table
-        context["media"] = table.media
         context["index_url"] = index_url
         context["is_paginated"] = bool(self.paginate_by)
         context["is_searchable"] = self.is_searchable


Test patch:
diff --git a/wagtail/snippets/tests/test_snippets.py b/wagtail/snippets/tests/test_snippets.py
--- a/wagtail/snippets/tests/test_snippets.py
+++ b/wagtail/snippets/tests/test_snippets.py
@@ -24,6 +24,7 @@
 from wagtail.admin.admin_url_finder import AdminURLFinder
 from wagtail.admin.forms import WagtailAdminModelForm
 from wagtail.admin.panels import FieldPanel, ObjectList, get_edit_handler
+from wagtail.admin.staticfiles import versioned_static
 from wagtail.blocks.field_block import FieldBlockAdapter
 from wagtail.models import Locale, ModelLogEntry, Revision
 from wagtail.signals import published, unpublished
@@ -531,6 +532,14 @@ def create_test_snippets(self):
         FilterableSnippet.objects.create(text="From Indonesia", country_code="ID")
         FilterableSnippet.objects.create(text="From the UK", country_code="UK")
 
+    def test_get_include_filters_form_media(self):
+        response = self.get()
+        html = response.content.decode()
+        datetime_js = versioned_static("wagtailadmin/js/date-time-chooser.js")
+
+        # The script file for the date time chooser should be included
+        self.assertTagInHTML(f'<script src="{datetime_js}"></script>', html)
+
     def test_unfiltered_no_results(self):
         response = self.get()
         add_url = reverse("wagtailsnippets_snippetstests_filterablesnippet:add")
diff --git a/wagtail/test/snippets/migrations/0009_filterablesnippet_some_date.py b/wagtail/test/snippets/migrations/0009_filterablesnippet_some_date.py
new file mode 100644
--- /dev/null
+++ b/wagtail/test/snippets/migrations/0009_filterablesnippet_some_date.py
@@ -0,0 +1,18 @@
+# Generated by Django 4.2b1 on 2023-03-21 14:17
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ("snippetstests", "0008_filterablesnippet"),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name="filterablesnippet",
+            name="some_date",
+            field=models.DateField(auto_now=True),
+        ),
+    ]
diff --git a/wagtail/test/snippets/models.py b/wagtail/test/snippets/models.py
--- a/wagtail/test/snippets/models.py
+++ b/wagtail/test/snippets/models.py
@@ -72,6 +72,7 @@ class CountryCode(models.TextChoices):
 
     text = models.CharField(max_length=255)
     country_code = models.CharField(max_length=2, choices=CountryCode.choices)
+    some_date = models.DateField(auto_now=True)
 
     search_fields = [
         index.SearchField("text"),
@@ -91,7 +92,7 @@ def get_foo_country_code(self):
 class FilterableSnippetFilterSet(WagtailFilterSet):
     class Meta:
         model = FilterableSnippet
-        fields = ["country_code"]
+        fields = ["country_code", "some_date"]
 
 
 @register_snippet