diff --git a/wagtail/core/models.py b/wagtail/core/models.py index 2d2670d62e..1017418597 100644 --- a/wagtail/core/models.py +++ b/wagtail/core/models.py @@ -1756,6 +1756,10 @@ class Page(AbstractPage, index.Indexed, ClusterableModel, metaclass=PageBase): :param log_action flag for logging the action. Pass None to skip logging. Can be passed an action string. Defaults to 'wagtail.copy' """ + + if self._state.adding: + raise RuntimeError('Page.copy() called on an unsaved page') + exclude_fields = self.default_exclude_fields_in_copy + self.exclude_fields_in_copy + (exclude_fields or []) specific_self = self.specific if keep_live: diff --git a/wagtail/core/tests/test_page_model.py b/wagtail/core/tests/test_page_model.py index b51862a60c..ff78b5d142 100644 --- a/wagtail/core/tests/test_page_model.py +++ b/wagtail/core/tests/test_page_model.py @@ -1484,6 +1484,12 @@ class TestCopyPage(TestCase): # reset excluded fields for future tests EventPage.exclude_fields_in_copy = [] + def test_copy_unsaved_page(self): + """Test that unsaved page will not be copied.""" + new_page = SimplePage(slug='testpurp', title='testpurpose') + with self.assertRaises(RuntimeError): + new_page.copy() + class TestCopyForTranslation(TestCase): fixtures = ['test.json']