diff --git a/opentech/apply/funds/models.py b/opentech/apply/funds/models.py index c585cd9c74a7b1074b548e8cfb8340c464fd5750..e665586d4c9840a1263c377c982ba37c4d3fbe76 100644 --- a/opentech/apply/funds/models.py +++ b/opentech/apply/funds/models.py @@ -143,7 +143,15 @@ class Round(AbstractStreamForm): Q(end_date__gte=self.start_date) ) - conflicting_rounds = Round.objects.child_of(self.parent_page).filter( + if 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 = Round.objects.child_of(self.parent_page) + else: + # don't need parent page, we are an actual object now. + base_query = Round.objects.sibling_of(self) + + conflicting_rounds = base_query.filter( conflict_query ).exclude(id=self.id) diff --git a/opentech/apply/funds/tests/test_models.py b/opentech/apply/funds/tests/test_models.py index 78a65c820ad2a1ace49c278370ddb0b8778ce70e..431676d67ef2af9934ae104923530950c794edf3 100644 --- a/opentech/apply/funds/tests/test_models.py +++ b/opentech/apply/funds/tests/test_models.py @@ -111,3 +111,14 @@ class TestRoundModel(TestCase): overlapping_start = existing_round.end_date - timedelta(1) with self.assertRaises(ValidationError): self.make_round(start_date=overlapping_start, end_date=None) + + def test_can_not_overlap_clean(self): + existing_round = self.make_round() + overlapping_start = existing_round.end_date - timedelta(1) + 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 + + with self.assertRaises(ValidationError): + new_round.clean()