Skip to content

Commit 315d2cc

Browse files
author
Tim Csitkovics
committed
fixup! Make compatible with Python 2.X
1 parent 9edff42 commit 315d2cc

File tree

2 files changed

+5
-8
lines changed

2 files changed

+5
-8
lines changed

example/tests/test_serializers.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@
55
from django.urls import reverse
66
from django.utils import timezone
77

8+
from example.models import Author, Blog, Entry
89
from rest_framework_json_api.serializers import ResourceIdentifierObjectSerializer, ModelSerializer
910
from rest_framework_json_api.utils import format_resource_type
1011

11-
from example.models import Author, Blog, Entry
12-
1312
pytestmark = pytest.mark.django_db
1413

1514

@@ -35,17 +34,14 @@ def setUp(self):
3534
def test_forward_relationship_not_loaded_when_not_included(self):
3635
MockRequest = namedtuple('Request', ['query_params'])
3736
request_without_includes = MockRequest({})
38-
to_representation_was_called = False
3937

4038
class BlogSerializer(ModelSerializer):
4139
class Meta:
4240
model = Blog
4341
fields = '__all__'
4442

4543
def to_representation(self, instance):
46-
nonlocal to_representation_was_called
47-
to_representation_was_called = True
48-
return super().to_representation(instance)
44+
raise Exception('to_representation of BlogSerializer was called')
4945

5046
class EntrySerializer(ModelSerializer):
5147
blog = BlogSerializer()
@@ -60,7 +56,6 @@ class Meta:
6056

6157
serializer = EntrySerializer(context={'request': request_without_includes})
6258
serializer.to_representation(self.entry)
63-
self.assertFalse(to_representation_was_called)
6459

6560
def test_data_in_correct_format_when_instantiated_with_blog_object(self):
6661
serializer = ResourceIdentifierObjectSerializer(instance=self.blog)

rest_framework_json_api/serializers.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,9 @@ def to_representation(self, instance):
192192
def _get_field_representation(self, field, instance):
193193
request = self.context.get('request', None)
194194
is_included = field.source in get_included_resources(request)
195-
if not is_included and isinstance(field, ModelSerializer) and hasattr(instance, field.source + '_id'):
195+
if not is_included and \
196+
isinstance(field, ModelSerializer) and \
197+
hasattr(instance, field.source + '_id'):
196198
attribute = getattr(instance, field.source + '_id')
197199

198200
if attribute is None:

0 commit comments

Comments
 (0)