Django REST framework HyperlinkedIdentityField format kwarg not working

Viewed 84

I have just started looking into Django REST Framework and am following the tutorial in the official documentation. In this tutorial, we use HyperLinkedIdentityField for highlight attribute. We set the format kwarg to html. The tutorial states:

Notice that we've also added a new 'highlight' field. This field is of the same type as the url field, except that it points to the 'snippet-highlight' url pattern, instead of the 'snippet-detail' url pattern.

Because we've included format suffixed URLs such as '.json', we also need to indicate on the highlight field that any format suffixed hyperlinks it returns should use the '.html' suffix.

The problem is that when I visit the snippet-list using the url http://127.0.0.1:8000/snippets/?format=json I get the highlight url as http://127.0.0.1:8000/snippets/1/highlight/?format=json while I expected it to be http://127.0.0.1:8000/snippets/1/highlight/?format=html because no matter what format we request the snippet-list url, the highlight link will always have HTML format. My code looks like this:

highlight = serializers.HyperlinkedIdentityField(view_name='snippet-highlight', format='html')


class Meta:
    model = Snippet
    fields = ['url', 'id', 'highlight', 'owner',
              'title', 'code', 'linenos', 'language', 'style']

Any help will be highly appreciated Thanks,

0 Answers
Related