Repository: wagtail/wagtail

Issue:
Exception Value:	 Model instances passed to related filters must be saved.
When building a form in wagtail==5.2.2, an Exception Value Error 'Model instances passed to related filters must be saved was raised. 
Django version ==5.0

### Issue Summary
My Model is as follows:
```python
class ContactFormField(AbstractFormField):
    page = ParentalKey('ContactFormPage', on_delete=models.CASCADE, related_name='contat_form_fields')

class ContactFormPage(AbstractEmailForm):
    template = 'home/connect.html'
    intro = RichTextField(blank=True)
    thank_you_text = RichTextField(blank=True)
    content_panels = AbstractEmailForm.content_panels + [
        FormSubmissionsPanel(),
        FieldPanel('intro'),
        InlinePanel('contat_form_fields', label="Form fields"),
        FieldPanel('thank_you_text'),
        MultiFieldPanel([
            FieldRowPanel([
                FieldPanel('from_address', classname="col6"),
                FieldPanel('to_address', classname="col6"),
            ]),
            FieldPanel('subject'),
        ], "Email"),
    ]

    def serve(self, request, *args, **kwargs):
        if request.method == 'POST':
            form = self.get_form(request.POST, page=self, user=request.user)

            if form.is_valid():
                self.process_form_submission(form)
                
                # Update the original landing page context with other data
                landing_page_context = self.get_context(request)
                landing_page_context['email'] = form.cleaned_data['email']

                return render(
                    request,
                    self.get_landing_page_template(request),
                    landing_page_context
                )
        else:
            form = self.get_form(page=self, user=request.user)

        context = self.get_context(request)
        context['form'] = form
        return render(
            request,
            self.get_template(request),
            context
        )
```

### Steps to Reproduce

1. (for example) Start a new project with `wagtail start myproject`
2. Create a form in Model following the steps at https://docs.wagtail.org/en/stable/reference/contrib/forms/index.html.
3. Navigate to Wagtail admin and create a new form page.

### Technical details

-   Python version: Run `python --version`.
-   Django version: Look in your requirements.txt, or run `pip show django | grep Version`.
-   Wagtail version: Look at the bottom of the Settings menu in the Wagtail admin, or run `pip show wagtail | grep Version:`.
-   Browser version: You can use https://www.whatsmybrowser.org/ to find this out.



Discussion:
Thanks for the report @LekiaAnonim! I can confirm that I'm seeing the same issue when running on Django 5, with any form using `FormSubmissionsPanel()` (the `serve` method is not necessary here). Tested with a fresh Wagtail instance with the following in `home/models.py`:

```python
from django.db import models

from wagtail.fields import RichTextField
from wagtail.models import Page
from wagtail.contrib.forms.models import AbstractFormField, AbstractEmailForm
from modelcluster.fields import ParentalKey
from wagtail.admin.panels import FieldPanel, InlinePanel, MultiFieldPanel, FieldRowPanel
from wagtail.contrib.forms.panels import FormSubmissionsPanel


class HomePage(Page):
    pass


class ContactFormField(AbstractFormField):
    page = ParentalKey('ContactFormPage', on_delete=models.CASCADE, related_name='contat_form_fields')

class ContactFormPage(AbstractEmailForm):
    intro = RichTextField(blank=True)
    thank_you_text = RichTextField(blank=True)
    content_panels = AbstractEmailForm.content_panels + [
        FormSubmissionsPanel(),
        FieldPanel('intro'),
        InlinePanel('contat_form_fields', label="Form fields"),
        FieldPanel('thank_you_text'),
        MultiFieldPanel([
            FieldRowPanel([
                FieldPanel('from_address', classname="col6"),
                FieldPanel('to_address', classname="col6"),
            ]),
            FieldPanel('subject'),
        ], "Email"),
    ]
```

Resolving patch:
diff --git a/wagtail/contrib/forms/panels.py b/wagtail/contrib/forms/panels.py
--- a/wagtail/contrib/forms/panels.py
+++ b/wagtail/contrib/forms/panels.py
@@ -18,7 +18,11 @@ class BoundPanel(Panel.BoundPanel):
         def submissions(self):
             form_page_model = self.panel.model
             form_submissions_model = form_page_model().get_submission_class()
-            return form_submissions_model.objects.filter(page=self.instance)
+            if self.instance.pk:
+                return form_submissions_model.objects.filter(page=self.instance)
+            else:
+                # Page has not been created yet, so there can't be any submissions
+                return form_submissions_model.objects.none()
 
         @cached_property
         def submission_count(self):


Test patch:
diff --git a/wagtail/contrib/forms/tests/test_views.py b/wagtail/contrib/forms/tests/test_views.py
--- a/wagtail/contrib/forms/tests/test_views.py
+++ b/wagtail/contrib/forms/tests/test_views.py
@@ -76,6 +76,30 @@ def test_render_without_submissions(self):
         self.assertFalse(self.panel.is_shown())
 
 
+class TestFormResponsesPanelWithNewPage(TestCase):
+    def setUp(self):
+        self.request = RequestFactory().get("/")
+        user = AnonymousUser()  # technically, Anonymous users cannot access the admin
+        self.request.user = user
+
+        self.form_page = FormPage()
+
+        self.FormPageForm = get_form_for_model(
+            FormPage,
+            form_class=WagtailAdminPageForm,
+            fields=["title", "slug", "to_address", "from_address", "subject"],
+        )
+
+        panel = FormSubmissionsPanel().bind_to_model(FormPage)
+        self.panel = panel.get_bound_panel(
+            instance=self.form_page, form=self.FormPageForm(), request=self.request
+        )
+
+    def test_render_without_submissions(self):
+        """The panel should not be shown if the number of submission is zero."""
+        self.assertFalse(self.panel.is_shown())
+
+
 class TestFormResponsesPanelWithCustomSubmissionClass(WagtailTestUtils, TestCase):
     def setUp(self):
         self.request = RequestFactory().get("/")
@@ -1822,3 +1846,85 @@ def test_preview_modes(self):
                 response = self.client.get(preview_url + params)
                 self.assertEqual(response.status_code, 200)
                 self.assertTemplateUsed(response, template)
+
+
+class TestFormPageCreate(WagtailTestUtils, TestCase):
+    def setUp(self):
+        # Find root page
+        self.root_page = Page.objects.get(id=2)
+
+        # Login
+        self.user = self.login()
+
+    def test_get_creation_form(self):
+        response = self.client.get(
+            reverse(
+                "wagtailadmin_pages:add",
+                args=("tests", "formpage", self.root_page.id),
+            )
+        )
+        self.assertEqual(response.status_code, 200)
+
+    def test_post_creation_form(self):
+        post_data = {
+            "title": "Form page!",
+            "slug": "contact-us",
+            "form_fields-TOTAL_FORMS": "1",
+            "form_fields-INITIAL_FORMS": "1",
+            "form_fields-MIN_NUM_FORMS": "0",
+            "form_fields-MAX_NUM_FORMS": "1000",
+            "form_fields-0-id": "",
+            "form_fields-0-label": "Field One",
+            "form_fields-0-field_type": "singleline",
+        }
+        response = self.client.post(
+            reverse(
+                "wagtailadmin_pages:add",
+                args=("tests", "formpage", self.root_page.id),
+            ),
+            post_data,
+        )
+        # Find the page and check it
+        page = Page.objects.get(slug="contact-us")
+
+        # Should be redirected to edit page
+        self.assertRedirects(
+            response, reverse("wagtailadmin_pages:edit", args=(page.id,))
+        )
+
+
+class TestFormPageEdit(WagtailTestUtils, TestCase):
+    def setUp(self):
+        self.form_page = make_form_page()
+
+        # Login
+        self.user = self.login()
+
+    def test_get_edit_form(self):
+        response = self.client.get(
+            reverse("wagtailadmin_pages:edit", args=(self.form_page.id,))
+        )
+        self.assertEqual(response.status_code, 200)
+
+    def test_post_edit_form(self):
+        post_data = {
+            "title": "Updated form page",
+            "slug": "contact-us",
+            "form_fields-TOTAL_FORMS": "1",
+            "form_fields-INITIAL_FORMS": "1",
+            "form_fields-MIN_NUM_FORMS": "0",
+            "form_fields-MAX_NUM_FORMS": "1000",
+            "form_fields-0-id": "",
+            "form_fields-0-label": "Field One",
+            "form_fields-0-field_type": "singleline",
+        }
+        response = self.client.post(
+            reverse("wagtailadmin_pages:edit", args=(self.form_page.id,)),
+            post_data,
+        )
+        # Should be redirected to edit page
+        self.assertRedirects(
+            response, reverse("wagtailadmin_pages:edit", args=(self.form_page.id,))
+        )
+        page = Page.objects.get(slug="contact-us").get_latest_revision_as_object()
+        self.assertEqual(page.title, "Updated form page")
diff --git a/wagtail/test/testapp/models.py b/wagtail/test/testapp/models.py
--- a/wagtail/test/testapp/models.py
+++ b/wagtail/test/testapp/models.py
@@ -49,6 +49,7 @@
     AbstractFormField,
     AbstractFormSubmission,
 )
+from wagtail.contrib.forms.panels import FormSubmissionsPanel
 from wagtail.contrib.forms.views import SubmissionsListView
 from wagtail.contrib.settings.models import (
     BaseGenericSetting,
@@ -569,6 +570,7 @@ def get_context(self, request):
             ],
             "Email",
         ),
+        FormSubmissionsPanel(),
     ]