Skip to content

Commit

Permalink
Improve performance of extract_related()
Browse files Browse the repository at this point in the history
Check more thoroughly if the related resource is already included
in the response, and skip serialization if it is.
  • Loading branch information
juyrjola committed Feb 23, 2019
1 parent 75e2f82 commit 4f3b5d8
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
1 change: 1 addition & 0 deletions AUTHORS
Expand Up @@ -23,3 +23,4 @@ Mohammed Ali Zubair <mazg1493@gmail.com>
Jason Housley <housleyjk@gmail.com>
Beni Keller <beni@matraxi.ch>
Stas S. <stas@nerd.ro>
Juha Yrjölä <juha.yrjola@iki.fi>
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -31,6 +31,7 @@ any parts of the framework not mentioned in the documentation should generally b
* 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

Expand Down
9 changes: 8 additions & 1 deletion rest_framework_json_api/renderers.py
Expand Up @@ -408,6 +408,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
Expand All @@ -432,7 +436,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:
Expand Down

0 comments on commit 4f3b5d8

Please sign in to comment.