Skip to content

Commit 2227acf

Browse files
author
Tim Csitkovics
committed
Do not lazy load forward relations
If the forward relations are included, the renderer will break, so this optimization only works if it is not included.
1 parent 96c533b commit 2227acf

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

rest_framework_json_api/serializers.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,10 @@ def to_representation(self, instance):
182182

183183
for field in readable_fields:
184184
try:
185-
186-
if isinstance(field, ModelSerializer) and hasattr(field, field.source + "_id"):
185+
context = getattr(self, '_context', {})
186+
request = getattr(context.get('request', None), '_request', None)
187+
is_included = request is not None and field.source in getattr(request, 'GET', None)
188+
if not is_included and isinstance(field, ModelSerializer) and hasattr(instance, field.source + "_id"):
187189
attribute = getattr(instance, field.source + "_id")
188190
if attribute is None:
189191
ret[field.field_name] = None

0 commit comments

Comments
 (0)