Skip to content

Commit dd2506c

Browse files
committed
Improve performance of extract_related()
Check more thoroughly if the related resource is already included in the response, and skip serialization if it is.
1 parent 5650bd1 commit dd2506c

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,6 @@ Mohammed Ali Zubair <mazg1493@gmail.com>
2323
Jason Housley <housleyjk@gmail.com>
2424
Beni Keller <beni@matraxi.ch>
2525
Stas S. <stas@nerd.ro>
26+
Juha Yrjölä <juha.yrjola@iki.fi>
2627
Nathanael Gordon <nathanael.l.gordon@gmail.com>
2728
Charlie Allatson <charles.allatson@gmail.com>

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ This is the last release supporting Python 2.7, Python 3.4, Django Filter 1.1, D
6363
* 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)
6464
* 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))
6565
* 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))
66+
* Performance improvement when rendering included data
6667

6768
## [2.6.0] - 2018-09-20
6869

rest_framework_json_api/renderers.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,10 @@ def extract_included(cls, fields, resource, resource_instance, included_resource
397397
relation_type or
398398
utils.get_resource_type_from_instance(nested_resource_instance)
399399
)
400+
resource_id = encoding.force_text(nested_resource_instance.pk)
401+
if included_cache.get(resource_type, {}).get(resource_id):
402+
# Do not serialize if already included in the response
403+
continue
400404
serializer_fields = utils.get_serializer_fields(
401405
serializer.__class__(
402406
nested_resource_instance, context=serializer.context
@@ -421,7 +425,10 @@ def extract_included(cls, fields, resource, resource_instance, included_resource
421425

422426
if isinstance(field, Serializer):
423427
relation_type = utils.get_resource_type_from_serializer(field)
424-
428+
resource_id = encoding.force_text(relation_instance.pk)
429+
if included_cache.get(relation_type, {}).get(resource_id):
430+
# Do not serialize if already included in the response
431+
continue
425432
# Get the serializer fields
426433
serializer_fields = utils.get_serializer_fields(field)
427434
if serializer_data:

0 commit comments

Comments
 (0)