I’ve got this Serializer translating path to url, and it works on its own, but when i try to nest this serializer inside another serializer, i get this error. Do you have any idea why? I need to have this function, because otherwise it just shows the paths to the image in this main SpecialistSerializer.
JavaScript
x
13
13
1
class EntityPhotosSerializer(serializers.ModelSerializer):
2
image = serializers.SerializerMethodField('get_file_abs_url')
3
4
5
class Meta:
6
model = EntityPhoto
7
fields = ('user', 'entity', 'image',)
8
9
def get_file_abs_url(self, obj):
10
request = self.context.get('request')
11
return request.build_absolute_uri(obj.image.url)
12
13
JavaScript
1
12
12
1
class SpecialistSerializer(serializers.ModelSerializer):
2
reviews_quantity = serializers.IntegerField(source="get_reviews_quantity")
3
4
class Meta:
5
model = Entity
6
fields = '__all__'
7
8
def to_representation(self, instance):
9
data = super().to_representation(instance)
10
data['photos'] = EntityPhotosSerializer(many=True, instance=instance.entityphoto_set.all()).data
11
return data
12
Traceback:
JavaScript
1
43
43
1
Traceback (most recent call last):
2
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/core/handlers/exception.py", line 47, in inner
3
response = get_response(request)
4
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/core/handlers/base.py", line 179, in _get_response
5
response = wrapped_callback(request, *callback_args, **callback_kwargs)
6
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
7
return view_func(*args, **kwargs)
8
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/views/generic/base.py", line 70, in view
9
return self.dispatch(request, *args, **kwargs)
10
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/rest_framework/views.py", line 509, in dispatch
11
response = self.handle_exception(exc)
12
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/rest_framework/views.py", line 469, in handle_exception
13
self.raise_uncaught_exception(exc)
14
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
15
raise exc
16
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/rest_framework/views.py", line 506, in dispatch
17
response = handler(request, *args, **kwargs)
18
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/rest_framework/generics.py", line 282, in get
19
return self.retrieve(request, *args, **kwargs)
20
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/rest_framework/mixins.py", line 56, in retrieve
21
return Response(serializer.data)
22
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/rest_framework/serializers.py", line 548, in data
23
ret = super().data
24
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/rest_framework/serializers.py", line 246, in data
25
self._data = self.to_representation(self.instance)
26
File "/Users/jakubstrawa/programming/PythonKuba/api/specialists/serializers.py", line 35, in to_representation
27
data['photos'] = EntityPhotosSerializer(many=True, instance=instance.entityphoto_set.all()).data
28
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/rest_framework/serializers.py", line 745, in data
29
ret = super().data
30
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/rest_framework/serializers.py", line 246, in data
31
self._data = self.to_representation(self.instance)
32
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/rest_framework/serializers.py", line 664, in to_representation
33
self.child.to_representation(item) for item in iterable
34
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/rest_framework/serializers.py", line 664, in <listcomp>
35
self.child.to_representation(item) for item in iterable
36
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/rest_framework/serializers.py", line 515, in to_representation
37
ret[field.field_name] = field.to_representation(attribute)
38
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/rest_framework/fields.py", line 1870, in to_representation
39
return method(value)
40
File "/Users/jakubstrawa/programming/PythonKuba/api/specialists/serializers.py", line 23, in get_file_abs_url
41
return request.build_absolute_uri(obj.image.url)
42
AttributeError: 'NoneType' object has no attribute 'build_absolute_uri'
43
JavaScript
1
1
1
Advertisement
Answer
In to_representation
, you access to the EntityPhotosSerializer
. And in that serializer, you are trying to access to the context. If you want to do that, you need to pass the current context to EntityPhotosSerializer
in SpecialistSerializer
.
JavaScript
1
5
1
def to_representation(self, instance):
2
data = super().to_representation(instance)
3
data['photos'] = EntityPhotosSerializer(many=True, context=self.context, instance=instance.entityphoto_set.all()).data
4
return data
5