5👍
Was able to figure out looking into to_representation
implementation of FileField
.
class ImageUrlField(serializers.RelatedField):
def to_representation(self, instance):
url = instance.image.url
request = self.context.get('request', None)
if request is not None:
return request.build_absolute_uri(url)
return url
class PostSerialiser(serializers.ModelSerializer):
name = serializers.CharField()
images = serializers.ImageUrlField(many=True, read_only=True)
👤NEB
0👍
I don’t have much experience with ImageFields, so there may be another solution, but for sure this will work:
class ImageUrlField(serializers.RelatedField):
def to_representation(self, value):
# Build absolute URL (next line is just sample code)
str = settings.MEDIA_URL + str(value.image)
return str
class PostSerialiser(serializers.ModelSerializer):
name = serializers.CharField()
images = serializers.ImageUrlField(many=True, read_only=True)
- [Django]-Django 1.6b gis import error
- [Django]-Django: Access and Update fields of a Form Class inside __init__ or save functions
- [Django]-Using mongoengine with models.ImageField
Source:stackexchange.com