Want a relative path of the image to be returned from the API:
Want "/media/image.jpg
" format, not "http://0.0.0.1:8000/media.image.jpg
" to be returned for the API
For the following, getting full URL i.e. "http://0.0.0.1:8000/media.image.jpg
" format
class ImageListAPIView(ListAPIView):
serializer_class = QuestionImageSerializer
queryset = QuestionImage.objects.all()
And for the following, getting correct output i.e.: media/image.jpg
format, which is required.
class ImageAPIView(APIView):
def get(self, request, format=None):
QuestionImages = QuestionImage.objects.all()
SerializedData = QuestionImageSerializer(QuestionImages, many=True)
return Response({
'QeustionImages:': SerializedData.data
})
How to return the relative path URL for the ListAPIView?
Tried putting following in the serializer, but then not able to upload the image to the API.
image = serializers.SerializerMethodField()
def get_image(self, obj):
return obj.image.url
PS:
models.py:
class QuestionImage(models.Model):
image = models.ImageField(upload_to=question_directory_path)
serializers.py:
class QuestionImageSerializer(ModelSerializer):
class Meta:
model = QuestionImage
fields= [
'id',
'image',
]