Created
October 31, 2014 18:36
-
-
Save blapid/fcf6f7d16036d6cc4de3 to your computer and use it in GitHub Desktop.
Django Rest Framework ImageFileField Serializer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
When ImageFields are serialized with Djang Rest Framework you only get | |
the URN of the file. So I made this serializer which adds the rest | |
of the URI. | |
''' | |
class ImageFileFieldSerializer(serializers.WritableField): | |
def __init__(self, *args, **kwargs): | |
super(ImageFileFieldSerializer, self).__init__(*args, **kwargs) | |
def to_native(self, obj): | |
request = self.context.get('request', None) | |
full_uri = "http://" + request.get_host() + "/" + obj.url | |
return full_uri |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment