Django Rest Framework – Custom Hyperlink field in serializer
<p>How can I add a custom hyperlink field in a serializer? I would like to have a hyperlink field in my serializer that has query params in it. Since there is no way to pass query params from <code>HyperlinkedRelatedField</code> or <code>HyperlinkedIdentityField</code> as far as I know, I've tried using a <code>SerializerMethodField</code>. However, this only serializes to a string, and is not a clickable URL when I visit the API through my browser. My code looks something like this:</p>

<pre><code>class MySerializer(serializers.HyperlinkedModelSerializer):
    custom_field = serializers.SerializerMethodField()

    class Meta:
        model = MyModel
        fields = ('url', 'custom_field')

    def get_custom_field(self, obj):
        result = '{}?{}'.format(
            reverse('my-view'),
            urllib.urlencode({'param': 'foo'})
        )
        return result
</code></pre>

<p>Also, I am having trouble understanding the difference between a <code>HyperlinkedRelatedField</code> and a <code>HyperlinkedIdentityField</code>, so a brief explanation would be appreciated.</p>