Skip to content

Instantly share code, notes, and snippets.

@blapid
Created October 31, 2014 18:36
Show Gist options
  • Save blapid/fcf6f7d16036d6cc4de3 to your computer and use it in GitHub Desktop.
Save blapid/fcf6f7d16036d6cc4de3 to your computer and use it in GitHub Desktop.
Django Rest Framework ImageFileField Serializer
'''
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