[PATCH 1/6] REST: Correct some prefetch, select_related

Stephen Finucane stephen at that.guru
Tue May 16 09:14:30 AEST 2017


There were two issues here:

- The 'get_queryset' function, rather than the 'queryset' attribute,
  must be overriden when using either the 'prefetch_related' or
  'select_related' functions

- A couple of endpoints contained a 'project' attribute, but this wasn't
  being prefetched. This didn't cause issues in a single-project
  deployment, as used in testing, but will for larger deployments

Resolve both issues.

Signed-off-by: Stephen Finucane <stephen at that.guru>
---
 patchwork/api/cover.py   | 4 ++--
 patchwork/api/patch.py   | 6 ++++--
 patchwork/api/person.py  | 4 +++-
 patchwork/api/project.py | 4 +++-
 4 files changed, 12 insertions(+), 6 deletions(-)

diff --git a/patchwork/api/cover.py b/patchwork/api/cover.py
index 8b8d47b..e45680b 100644
--- a/patchwork/api/cover.py
+++ b/patchwork/api/cover.py
@@ -73,7 +73,7 @@ class CoverLetterList(ListAPIView):
 
     def get_queryset(self):
         qs = CoverLetter.objects.all().prefetch_related('series')\
-            .select_related('submitter')
+            .select_related('project', 'submitter')
 
         # FIXME(stephenfin): This causes issues with Django 1.6 for whatever
         # reason. Suffer the performance hit on those versions.
@@ -90,4 +90,4 @@ class CoverLetterDetail(RetrieveAPIView):
 
     def get_queryset(self):
         return CoverLetter.objects.all().prefetch_related('series')\
-            .select_related('submitter')
+            .select_related('project', 'submitter')
diff --git a/patchwork/api/patch.py b/patchwork/api/patch.py
index 1e5c13f..294c5f6 100644
--- a/patchwork/api/patch.py
+++ b/patchwork/api/patch.py
@@ -147,9 +147,11 @@ class PatchList(ListAPIView):
                        'submitter', 'check')
 
     def get_queryset(self):
+        # TODO(stephenfin): Does the defer here cause issues with Django 1.6
+        # (like /cover)?
         return Patch.objects.all().with_tag_counts()\
             .prefetch_related('series', 'check_set')\
-            .select_related('state', 'submitter', 'delegate')\
+            .select_related('project', 'state', 'submitter', 'delegate')\
             .defer('content', 'diff', 'headers')
 
 
@@ -162,4 +164,4 @@ class PatchDetail(RetrieveUpdateAPIView):
     def get_queryset(self):
         return Patch.objects.all().with_tag_counts()\
             .prefetch_related('series', 'check_set')\
-            .select_related('state', 'submitter', 'delegate')
+            .select_related('project', 'state', 'submitter', 'delegate')
diff --git a/patchwork/api/person.py b/patchwork/api/person.py
index 7947f82..574fa84 100644
--- a/patchwork/api/person.py
+++ b/patchwork/api/person.py
@@ -38,10 +38,12 @@ class PersonSerializer(HyperlinkedModelSerializer):
 
 class PersonMixin(object):
 
-    queryset = Person.objects.prefetch_related('user')
     permission_classes = (IsAuthenticated,)
     serializer_class = PersonSerializer
 
+    def get_queryset(self):
+        return Person.objects.all().prefetch_related('user')
+
 
 class PersonList(PersonMixin, ListAPIView):
     """List users."""
diff --git a/patchwork/api/project.py b/patchwork/api/project.py
index ce3ed58..8fb8984 100644
--- a/patchwork/api/project.py
+++ b/patchwork/api/project.py
@@ -48,7 +48,6 @@ class ProjectSerializer(HyperlinkedModelSerializer):
 
 class ProjectMixin(object):
 
-    queryset = Project.objects.all()
     permission_classes = (PatchworkPermission,)
     serializer_class = ProjectSerializer
 
@@ -70,6 +69,9 @@ class ProjectMixin(object):
 
         return obj
 
+    def get_queryset(self):
+        return Project.objects.all().prefetch_related('maintainer_project')
+
 
 class ProjectList(ProjectMixin, ListAPIView):
     """List projects."""
-- 
2.9.3



More information about the Patchwork mailing list