diff --git a/opentech/apply/funds/models/applications.py b/opentech/apply/funds/models/applications.py index 13bb3ace98821370003337962e0c5719d22d25dc..fccac8966385c609bd0f786aeb07ebbf0756f3f4 100644 --- a/opentech/apply/funds/models/applications.py +++ b/opentech/apply/funds/models/applications.py @@ -162,14 +162,14 @@ class RoundBase(WorkflowStreamForm, SubmittableStreamForm): # type: ignore def save(self, *args, **kwargs): is_new = not self.id - if is_new: + if is_new and hasattr(self, 'parent_page'): parent_page = self.parent_page[self.__class__][self.title] self.workflow_name = parent_page.workflow_name self.reviewers = parent_page.reviewers.all() super().save(*args, **kwargs) - if is_new: + if is_new and hasattr(self, 'parent_page'): # Would be nice to do this using model clusters as part of the __init__ self._copy_forms('forms') self._copy_forms('review_forms') @@ -214,7 +214,7 @@ class RoundBase(WorkflowStreamForm, SubmittableStreamForm): # type: ignore Q(end_date__gte=self.start_date) ) - if not self.id: + if not self.id and hasattr(self, 'parent_page'): # Check if the create hook has added the parent page, we aren't an object yet. # Ensures we can access related objects during the clean phase instead of save. base_query = RoundBase.objects.child_of(self.parent_page[self.__class__][self.title]) diff --git a/opentech/apply/funds/tests/test_models.py b/opentech/apply/funds/tests/test_models.py index 10e29363d5bccd2bfc70ba47f2fadb94e7dfe6d8..28842606bac340e3f2aa28d447d4e0e1b0e1227f 100644 --- a/opentech/apply/funds/tests/test_models.py +++ b/opentech/apply/funds/tests/test_models.py @@ -138,7 +138,7 @@ class TestRoundModelDates(TestCase): new_round = RoundFactory.build(start_date=overlapping_start, end_date=None) # we add on the parent page which gets included from a pre_create_hook - new_round.parent_page = self.fund + new_round.parent_page = {new_round.__class__: {new_round.title: self.fund}} with self.assertRaises(ValidationError): new_round.clean() @@ -153,7 +153,7 @@ class TestRoundModelWorkflowAndForms(TestCase): self.round = RoundFactory.build(lead=lead, parent=None) # Assign parent_page like the init does - self.round.parent_page = self.fund + self.round.parent_page = {self.round.__class__: {self.round.title: self.fund}} self.fund.add_child(instance=self.round) def test_workflow_is_copied_to_new_rounds(self):