Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django REST Framework and FileField absolute url

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.

like image 947
Mark Semsel Avatar asked May 28 '14 17:05

Mark Semsel


People also ask

How can get image URL in Django REST framework?

To get the full URL of the image value of a Python Django REST framework Imagefield, we can get it from the image value's url property. to create the CarSerializer that get the URL of the car.

What is absolute URL Django?

To get the full or absolute URL (with domain) in Python Django, we can use the build_absolute_uri method. request.build_absolute_uri(reverse('view_name', args=(obj.pk, ))) to call request. build_absolute_uri with reverse('view_name', args=(obj.pk, ) to get the path of the view with reverse . Then we call “request.

Can I use Django and Django REST framework together?

Django Rest Framework makes it easy to use your Django Server as an REST API. REST stands for "representational state transfer" and API stands for application programming interface. Note that with DRF you easily have list and create views as well as authentication.

Is Django different from Django REST framework?

Django is the web development framework in python whereas the Django Rest Framework is the library used in Django to build Rest APIs. Django Rest Framework is especially designed to make the CRUD operations easier to design in Django. Django Rest Framework makes it easy to use your Django Server as an REST API.


1 Answers

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}) 
like image 83
johntellsall Avatar answered Sep 27 '22 23:09

johntellsall