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.
class EntityPhotosSerializer(serializers.ModelSerializer): image = serializers.SerializerMethodField('get_file_abs_url') class Meta: model = EntityPhoto fields = ('user', 'entity', 'image',) def get_file_abs_url(self, obj): request = self.context.get('request') return request.build_absolute_uri(obj.image.url)
class SpecialistSerializer(serializers.ModelSerializer): reviews_quantity = serializers.IntegerField(source="get_reviews_quantity") class Meta: model = Entity fields = '__all__' def to_representation(self, instance): data = super().to_representation(instance) data['photos'] = EntityPhotosSerializer(many=True, instance=instance.entityphoto_set.all()).data return data
Traceback:
Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/core/handlers/exception.py", line 47, in inner response = get_response(request) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/core/handlers/base.py", line 179, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view return view_func(*args, **kwargs) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/views/generic/base.py", line 70, in view return self.dispatch(request, *args, **kwargs) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/rest_framework/views.py", line 509, in dispatch response = self.handle_exception(exc) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/rest_framework/views.py", line 469, in handle_exception self.raise_uncaught_exception(exc) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception raise exc File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/rest_framework/views.py", line 506, in dispatch response = handler(request, *args, **kwargs) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/rest_framework/generics.py", line 282, in get return self.retrieve(request, *args, **kwargs) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/rest_framework/mixins.py", line 56, in retrieve return Response(serializer.data) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/rest_framework/serializers.py", line 548, in data ret = super().data File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/rest_framework/serializers.py", line 246, in data self._data = self.to_representation(self.instance) File "/Users/jakubstrawa/programming/PythonKuba/api/specialists/serializers.py", line 35, in to_representation data['photos'] = EntityPhotosSerializer(many=True, instance=instance.entityphoto_set.all()).data File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/rest_framework/serializers.py", line 745, in data ret = super().data File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/rest_framework/serializers.py", line 246, in data self._data = self.to_representation(self.instance) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/rest_framework/serializers.py", line 664, in to_representation self.child.to_representation(item) for item in iterable File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/rest_framework/serializers.py", line 664, in <listcomp> self.child.to_representation(item) for item in iterable File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/rest_framework/serializers.py", line 515, in to_representation ret[field.field_name] = field.to_representation(attribute) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/rest_framework/fields.py", line 1870, in to_representation return method(value) File "/Users/jakubstrawa/programming/PythonKuba/api/specialists/serializers.py", line 23, in get_file_abs_url return request.build_absolute_uri(obj.image.url) AttributeError: 'NoneType' object has no attribute 'build_absolute_uri'
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
.
def to_representation(self, instance): data = super().to_representation(instance) data['photos'] = EntityPhotosSerializer(many=True, context=self.context, instance=instance.entityphoto_set.all()).data return data