[PATCH] REST: Fix duplicate project queries

Stephen Finucane stephen at that.guru
Sun Feb 2 01:05:20 AEDT 2020


On Wed, 2020-01-29 at 20:01 +0100, Mete Polat wrote:
> 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 at gmail.com>

Hey Mete,

> ---
> There are still some duplicates in various /api/ views but it looks like
> those are caused by the REST framework itself.
> 
>  patchwork/api/cover.py  | 2 +-
>  patchwork/api/event.py  | 4 ++--
>  patchwork/api/patch.py  | 2 +-
>  patchwork/api/series.py | 5 +++--
>  4 files changed, 7 insertions(+), 6 deletions(-)
> 
> diff --git a/patchwork/api/cover.py b/patchwork/api/cover.py
> index caf9a386efa5..9e86d47e00e5 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 a066faaec63b..fdff6a4f2fa6 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')

The rest of these look good but I wasn't able to produce a test that
proved this particular change was doing anything. Are you sure this
particular change works and, if so, could you suggest one or more
scenarios that I could use to validate this?

Cheers,
Stephen

> diff --git a/patchwork/api/patch.py b/patchwork/api/patch.py
> index a29a1ab0eb71..1a3ce9057490 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 f7bb8c06a6c9..df28f95dab1b 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):



More information about the Patchwork mailing list