Django Restul return absolute URL's
<p>So I have a fairly straight forward serializer in <code>serializers.py</code></p>

<pre><code>class ScheduleSerializer(serializers.ModelSerializer):

    class Meta:
        model = FrozenSchedule
        fields = ['startDate', 'endDate', 'client', 'url']

    startDate = serializers.DateField(source='start_date')
    endDate = serializers.DateField(source='end_date')
    client = serializers.StringRelatedField(many=False)
    url = serializers.URLField(source='get_absolute_url')
</code></pre>

<p><code>get_absolute_url</code> in my <code>models.py</code></p>

<pre><code>def get_absolute_url(self):
    return reverse('reports:frozenschedule-detail', kwargs={
        'slug': self.client.slug, 'pk': self.id
    })
</code></pre>

<p>And it's related ViewSet in <code>viewsets.py</code></p>

<pre><code>class ScheduleViewSet(viewsets.ReadOnlyModelViewSet):
    queryset = FrozenSchedule.objects.not_abandoned().future()\
        .filter(signed=False).order_by('start_date')
    serializer_class = serializers.ScheduleSerializer
</code></pre>

<p>It returns JSON which looks like this:</p>

<pre><code> [
    {
        "startDate": "2016-10-01",
        "endDate": null,
        "client": "Abscissa.Com Limited",
        "url": "/clients/abscissac/frozenschedule/1",
    }
]
</code></pre>

<p>But I'd like it to return the complete URL, not just the relative path</p>

<pre><code>[
    {
        "startDate": "2016-10-01",
        "endDate": null,
        "client": "Abscissa.Com Limited",
        "url": "http://localhost:8000/clients/abscissac/frozenschedule/1",
    }
]
</code></pre>

<p>Could I serialize URL's this way inside my  Serializer?</p>

<p>The Restful documentation states that the rest_framework <code>reverse</code> function does exactly what I need. But it requires the request object to build the UR
<a href="http://www.django-rest-framework.org/api-guide/reverse/" rel="nofollow">http://www.django-rest-framework.org/api-guide/reverse/</a></p>

<p>Override HyperlinkedIdentityField.</p>

<pre><code>class UrlHyperlinkedIdentityField(HyperlinkedIdentityField):
   def get_url(self, obj, view_name, request, format):
     if obj.pk is None:
       return None

    return self.reverse(view_name,
        kwargs={
            'slug': obj.client.slug,
            'pk': obj.id,
        },
        request=request,
        format=format,
    )
</code></pre>

<p>Then in  serializers.py :</p>

<pre><code>url = UrlHyperlinkedIdentityField(view_name='reports:frozenschedule-detail')
</code></pre>

python django rest django-rest-framework