From dd2506cf276983aadf27c044ba1b1452cf121442 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juha=20Yrj=C3=B6l=C3=A4?= Date: Sat, 23 Feb 2019 11:37:44 +0200 Subject: [PATCH] Improve performance of extract_related() Check more thoroughly if the related resource is already included in the response, and skip serialization if it is. --- AUTHORS | 1 + CHANGELOG.md | 1 + rest_framework_json_api/renderers.py | 9 ++++++++- 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/AUTHORS b/AUTHORS index ff4d1dca..d39576e4 100644 --- a/AUTHORS +++ b/AUTHORS @@ -23,5 +23,6 @@ Mohammed Ali Zubair Jason Housley Beni Keller Stas S. +Juha Yrjölä Nathanael Gordon Charlie Allatson diff --git a/CHANGELOG.md b/CHANGELOG.md index 2e0c83dd..9471146d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -63,6 +63,7 @@ This is the last release supporting Python 2.7, Python 3.4, Django Filter 1.1, D * Allow `HyperlinkRelatedField` to be used with [related urls](https://django-rest-framework-json-api.readthedocs.io/en/stable/usage.html?highlight=related%20links#related-urls) * Avoid exception in `AutoPrefetchMixin` when including a reverse one to one relation ([#537](https://github.com/django-json-api/django-rest-framework-json-api/issues/537)) * Avoid requested resource(s) to be added to included as well ([#524](https://github.com/django-json-api/django-rest-framework-json-api/issues/524)) +* Performance improvement when rendering included data ## [2.6.0] - 2018-09-20 diff --git a/rest_framework_json_api/renderers.py b/rest_framework_json_api/renderers.py index ba41a061..3195fd17 100644 --- a/rest_framework_json_api/renderers.py +++ b/rest_framework_json_api/renderers.py @@ -397,6 +397,10 @@ def extract_included(cls, fields, resource, resource_instance, included_resource relation_type or utils.get_resource_type_from_instance(nested_resource_instance) ) + resource_id = encoding.force_text(nested_resource_instance.pk) + if included_cache.get(resource_type, {}).get(resource_id): + # Do not serialize if already included in the response + continue serializer_fields = utils.get_serializer_fields( serializer.__class__( nested_resource_instance, context=serializer.context @@ -421,7 +425,10 @@ def extract_included(cls, fields, resource, resource_instance, included_resource if isinstance(field, Serializer): relation_type = utils.get_resource_type_from_serializer(field) - + resource_id = encoding.force_text(relation_instance.pk) + if included_cache.get(relation_type, {}).get(resource_id): + # Do not serialize if already included in the response + continue # Get the serializer fields serializer_fields = utils.get_serializer_fields(field) if serializer_data: pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy