diff options
author | Mete Polat <metepolat2000@gmail.com> | 2020-01-29 20:01:22 +0100 |
---|---|---|
committer | Stephen Finucane <stephen@that.guru> | 2020-02-02 10:49:26 +0000 |
commit | ec00daae4d79bf2560034e1b2bc3cf76a98a3212 (patch) | |
tree | b22ee98d9a0283b3c219a94569e5640368e4a57d | |
parent | ab1c835fdf57e30effbc56ae6160bddb6969794c (diff) | |
download | patchwork-ec00daae4d79bf2560034e1b2bc3cf76a98a3212.tar patchwork-ec00daae4d79bf2560034e1b2bc3cf76a98a3212.tar.gz |
REST: Fix duplicate project queries
Eliminates duplicate project queries caused by calling
get_absolute_url() in the embedded serializers. Following foreign keys
with 'series__project' will cache the project of the series as well as
the series itself.
Signed-off-by: Mete Polat <metepolat2000@gmail.com>
Signed-off-by: Stephen Finucane <stephen@that.guru>
Closes: #335
-rw-r--r-- | patchwork/api/cover.py | 2 | ||||
-rw-r--r-- | patchwork/api/event.py | 4 | ||||
-rw-r--r-- | patchwork/api/patch.py | 2 | ||||
-rw-r--r-- | patchwork/api/series.py | 5 | ||||
-rw-r--r-- | patchwork/tests/api/test_cover.py | 3 | ||||
-rw-r--r-- | patchwork/tests/api/test_event.py | 3 | ||||
-rw-r--r-- | patchwork/tests/api/test_patch.py | 3 | ||||
-rw-r--r-- | patchwork/tests/api/test_series.py | 3 |
8 files changed, 11 insertions, 14 deletions
diff --git a/patchwork/api/cover.py b/patchwork/api/cover.py index caf9a38..9e86d47 100644 --- a/patchwork/api/cover.py +++ b/patchwork/api/cover.py @@ -101,7 +101,7 @@ class CoverLetterList(ListAPIView): def get_queryset(self): return CoverLetter.objects.all()\ - .select_related('project', 'submitter', 'series')\ + .select_related('project', 'submitter', 'series__project')\ .defer('content', 'headers') diff --git a/patchwork/api/event.py b/patchwork/api/event.py index a066faa..fdff6a4 100644 --- a/patchwork/api/event.py +++ b/patchwork/api/event.py @@ -86,7 +86,7 @@ class EventList(ListAPIView): def get_queryset(self): return Event.objects.all()\ - .prefetch_related('project', 'patch', 'series', 'cover', - 'previous_state', 'current_state', + .prefetch_related('project', 'patch__project', 'series__project', + 'cover', 'previous_state', 'current_state', 'previous_delegate', 'current_delegate', 'created_check') diff --git a/patchwork/api/patch.py b/patchwork/api/patch.py index a29a1ab..1a3ce90 100644 --- a/patchwork/api/patch.py +++ b/patchwork/api/patch.py @@ -176,7 +176,7 @@ class PatchList(ListAPIView): return Patch.objects.all()\ .prefetch_related('check_set')\ .select_related('project', 'state', 'submitter', 'delegate', - 'series')\ + 'series__project')\ .defer('content', 'diff', 'headers') diff --git a/patchwork/api/series.py b/patchwork/api/series.py index f7bb8c0..df28f95 100644 --- a/patchwork/api/series.py +++ b/patchwork/api/series.py @@ -55,8 +55,9 @@ class SeriesMixin(object): serializer_class = SeriesSerializer def get_queryset(self): - return Series.objects.all().prefetch_related('patches',)\ - .select_related('submitter', 'cover_letter', 'project') + return Series.objects.all()\ + .prefetch_related('patches__project',)\ + .select_related('submitter', 'cover_letter__project', 'project') class SeriesList(SeriesMixin, ListAPIView): diff --git a/patchwork/tests/api/test_cover.py b/patchwork/tests/api/test_cover.py index 891c554..16cc0cd 100644 --- a/patchwork/tests/api/test_cover.py +++ b/patchwork/tests/api/test_cover.py @@ -127,8 +127,7 @@ class TestCoverLetterAPI(utils.APITestCase): series = create_series() create_covers(5, series=series) - # FIXME(stephenfin): This should result in 2 queries - with self.assertNumQueries(3): + with self.assertNumQueries(2): self.client.get(self.api_url()) @utils.store_samples('cover-detail') diff --git a/patchwork/tests/api/test_event.py b/patchwork/tests/api/test_event.py index e0fca68..54935d0 100644 --- a/patchwork/tests/api/test_event.py +++ b/patchwork/tests/api/test_event.py @@ -183,8 +183,7 @@ class TestEventAPI(utils.APITestCase): for _ in range(3): self._create_events() - # FIXME(stephenfin): This should result in 28 queries - with self.assertNumQueries(32): + with self.assertNumQueries(28): self.client.get(self.api_url()) def test_order_by_date_default(self): diff --git a/patchwork/tests/api/test_patch.py b/patchwork/tests/api/test_patch.py index 888a257..6e5e6a0 100644 --- a/patchwork/tests/api/test_patch.py +++ b/patchwork/tests/api/test_patch.py @@ -215,8 +215,7 @@ class TestPatchAPI(utils.APITestCase): series = create_series() create_patches(5, series=series) - # FIXME(stephenfin): This should result in 3 queries - with self.assertNumQueries(8): + with self.assertNumQueries(3): self.client.get(self.api_url()) @utils.store_samples('patch-detail') diff --git a/patchwork/tests/api/test_series.py b/patchwork/tests/api/test_series.py index 4ad82fe..8751871 100644 --- a/patchwork/tests/api/test_series.py +++ b/patchwork/tests/api/test_series.py @@ -142,8 +142,7 @@ class TestSeriesAPI(utils.APITestCase): """Ensure we retrieve the embedded cover letter project once.""" self._create_series() - # FIXME(stephenfin): This should result in 4 queries - with self.assertNumQueries(5): + with self.assertNumQueries(4): self.client.get(self.api_url()) @utils.store_samples('series-detail') |