Django REST Framework and FileField absolute url
Asked Answered
F

11

39

I've defined a simple Django app that includes the following model:

class Project(models.Model):
    name = models.CharField(max_length=200)
    thumbnail = models.FileField(upload_to='media', null=True)

(Technically yes, that could have been an ImageField.)

In a template, it's easy enough to include the MEDIA_URL value (duly coded in settings.py) as a prefix to the thumbnail URL. The following works fine:

<div id="thumbnail"><img src="{{ MEDIA_URL }}{{ current_project.thumbnail }}" alt="thumbnail" width="400" height="300" border="0" /></div>

Using DRF, I've defined a HyperlinkedModelSerializer descendant called ProjectSerializer:

class ProjectSerializer(serializers.HyperlinkedModelSerializer):

    class Meta:
        model = Project
        fields = ( 'id' ,'url', 'name', 'thumbnail')

And I've defined a very straightforward ModelViewSet descendant:

class ProjectViewSet(viewsets.ModelViewSet):
    queryset = Project.objects.all()
    serializer_class = ProjectSerializer

A sample of the resulting JSON looks like this:

{
    "id": 1, 
    "url": "http://localhost:8000/api/v1/projects/1/", 
    "name": "Institutional", 
    "thumbnail": "media/institutional_thumb_1.jpg"
}

I have not yet been able to figure out how to provide a thumbnail field that includes the full url to the image in my project's JSON representation.

I would think that I would need to create a custom field in the ProjectSerializer, but have not been successful.

Fallible answered 28/5, 2014 at 17:54 Comment(0)
N
62

Try SerializerMethodField

Example (untested):

class MySerializer(serializers.ModelSerializer):
    thumbnail_url = serializers.SerializerMethodField('get_thumbnail_url')

    def get_thumbnail_url(self, obj):
        return self.context['request'].build_absolute_uri(obj.thumbnail_url)

The request must available to the serializer, so it can build the full absolute URL for you. One way is to explicitly pass it in when the serializer is created, similar to this:

serializer = MySerializer(account, context={'request': request})
Nutcracker answered 28/5, 2014 at 18:14 Comment(8)
How do you modify this to make it work with multiple images? Say he had a class called Preview that has a ForeignKey relation to Project. So Project has a preview_set and he wanted to achieve the same thing for each preview he had for the project. How do you achieve the same thing for each preview?Bereniceberenson
You don't need to pass the request. It is available already in self.context['view'].request.Baronetage
The problem with this is that SerializerMethodField is read-onlyBohemianism
In my case, just setting context={'request': request} when instantiating a serializer was enough to get the absolute URL. No need for SerializerMethodField.My
as @SerranoPereira said, for me it worked only with the contextBogosian
Isn't it supposed to be obj.thumbnail.url, instead of self.thumbnail_urlParalyse
This won't work if MEDIA_URL has a different domain.Skewbald
This answer made me realize that using context={'request': request} as serializer parameter, the file field will return the absolute URL if it's accessed from another domain.Geoponics
F
12

Thanks, shavenwarthog. Your example and documentation reference helped enormously. My implementation is slightly different, but very close to what you posted:

from SomeProject import settings

class ProjectSerializer(serializers.HyperlinkedModelSerializer):

    thumbnail_url = serializers.SerializerMethodField('get_thumbnail_url')

    def get_thumbnail_url(self, obj):
        return '%s%s' % (settings.MEDIA_URL, obj.thumbnail)

    class Meta:
        model = Project
        fields = ('id', 'url', 'name', 'thumbnail_url') 
Fallible answered 28/5, 2014 at 18:51 Comment(1)
You don't need to import setting.MEDIA_URL. Simply return: obj.thumbnail.urlLingerie
W
7

To get the url of a file which uses FileField you can just call the url attribute of the FieldFile (this is the file instance not the field), it use the Storage class to determine the url for this file. It's very straightforward if you are using a external storage like Amazon S3 or if your storage changes.

The get_thumbnail_url would be like this.

def get_thumbnail_url(self, obj):
    return obj.thumbnail.url

You can also use it in the template this way:

{{ current_project.thumbnail.url }}
Winkelman answered 6/8, 2014 at 20:1 Comment(0)
I
7

No need for any overrides or customizations. DRF handles it automatically. Take a look at to_representation method of FileField (source):

def to_representation(self, value):
    if not value:
        return None

    use_url = getattr(self, 'use_url', api_settings.UPLOADED_FILES_USE_URL)

    if use_url:
        if not getattr(value, 'url', None):
            # If the file has not been saved it may not have a URL.
            return None
        url = value.url
        request = self.context.get('request', None)
        if request is not None:
            return request.build_absolute_uri(url)
        return url
    return value.name

Note that it won't work if the context of the serializer is not set properly. If you're using ViewSets, no worries, everything is done silently but if you're instantiating the serializer manually you have to pass in the request in the context.

context = {'request': request}
serializer = ExampleSerializer(instance, context=context)
return Response(serializer.data)

https://www.django-rest-framework.org/community/3.0-announcement/#file-fields-as-urls

Irick answered 16/9, 2018 at 18:46 Comment(2)
The important bit here is to use FileField (not URLField) and add the request to the context.Unwinking
got issue with ListAPIView and your solution with context is working! thanks alotGerda
E
6

I found it annoying to write the same code for a serialized method field. If you have set correctly the MEDIA_ROOT to your S3 bucket URL, you can add a field to the serializer like:

class ProjectSerializer(serializers.ModelSerializer):
    logo_url = serializers.URLField(read_only=True, source='logo.url')

    class Meta:
        model = Project

logo is an ImageField in the model. it must not be nullable in order to avoid errors like ValueError: The 'img' attribute has no file associated with it.

I only use .build_absolute_uri in a serializer methodfield to return absolute urls that use other views in my API. for example, in my project there is an URL /webviews/projects/<pk> that shows, a title and a button that collects some user input (i.e. not exactly what you would do with suffixes, as it's not a plain representation of the resource but includes some logic instead). the end point /projects/<pk>/ contains a field "webview_url" ponting there, which is generated with SerializerMethodField. it's not media.

Expectant answered 22/2, 2015 at 18:1 Comment(0)
S
1

Just pass the context and pass request object. if you are using @api_view

serializer = CustomerSerializer(customer, context={"request": request})

For ViewSet user get_serializer_context method

class ProjectViewSet(viewsets.ModelViewSet):
queryset = Project.objects.all()
serializer_class = ProjectSerializer

def get_serializer_context(self):
    return {'request': self.request}
Sniff answered 19/12, 2016 at 7:56 Comment(0)
A
1

Just pass this "context={'request': request}" argument where you call your model serializer class to serialize the object. You can follow the below snippet to get the complete URL field.

serialized_object = serializers.MySerializer(data, many=true, context={'request': request})
Arana answered 4/4, 2020 at 14:12 Comment(0)
F
1

if you cannot access extra context in your serializer using Viewsets, try register your router with a basename in urls.py:

router.register('projects', ProjectViewSet, basename='project')

you can use build_absolute_uri:

def get_thumbnail_url(self, obj):
    return self.context.get('request').build_absolute_uri(obj.thumbnail.url)
Fishwife answered 2/11, 2020 at 18:3 Comment(0)
C
0

Check you settings.py

media settings.

I had same error and found that:

MEDIA_URL = '/media/' did the trick.

Before i only had:

MEDIA_URL = 'media/'

Circlet answered 7/11, 2016 at 19:28 Comment(0)
V
0

In my case, override to_representation method works right.

# models.py
class DailyLove(models.Model):
    content = models.CharField(max_length=1000)
    pic = models.FileField(upload_to='upload/api/media/DailyLove/')
    date = models.DateTimeField(auto_created=True)

    def __str__(self):
        return str(self.date)

# serializers.py
class DailyLoveSerializer(serializers.HyperlinkedModelSerializer):
    def to_representation(self, instance):
        representation = super(DailyLoveSerializer, self).to_representation(instance)
        representation['pic_url'] = self.context['request'].build_absolute_uri('/' + instance.pic.url)
        return representation

    class Meta:
        model = DailyLove
        fields = '__all__'

# views.py
class DailyLoveViewSet(viewsets.ModelViewSet):
    queryset = DailyLove.objects.all().order_by('-date')
    serializer_class = DailyLoveSerializer

# result
HTTP 200 OK
Allow: GET, POST, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

[
    {
        "url": "http://localhost:8088/daily/3/",
        "date": "2019-05-04T12:33:00+08:00",
        "content": "123",
        "pic": "http://localhost:8088/daily/upload/api/media/DailyLove/nitish-meena-37745-unsplash.jpg",
        "pic_url": "http://localhost:8088/upload/api/media/DailyLove/nitish-meena-37745-unsplash.jpg"
    }
]
Vainglorious answered 4/5, 2019 at 5:23 Comment(0)
D
0

check this!

class FileFieldWithLinkRepresentation(serializers.FileField):

    def to_representation(self, value):
        return create_link(value.url, self.context['request'])

and create_link method:

def create_link(path: str, request: Request):
    domain = request.META['HTTP_HOST']
    if not path.startswith('/', 0, 1):
        path = '/' + path
    return request.scheme + "://" + domain + path

you can use FileFieldWithLinkRepresentation in every class that needs hyperlink representation of a FileField.

Denadenae answered 9/6, 2020 at 18:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.