tomchristie/django-rest-framework — pull request #1058 by ericbuehl (2013-08-23 17:31:24+00:00)
Title: parameterize identity field serializer class to allow for easier subclassing

Pull #501 was closed with the resolution that this was out of scope. This small change makes it much easier to subclass HyperlinkedModelSerializer to force the use of relative URLs like so:

class RelativeHyperlinkedRelatedField(serializers.HyperlinkedRelatedField):
def get_url(self, obj, view_name, request, format):
# by omiting request we force all urls to be relative
return super(RelativeHyperlinkedRelatedField, self).get_url(obj, view_name, None, format)

class RelativeHyperlinkedIdentityField(serializers.HyperlinkedIdentityField):
def get_url(self, obj, view_name, request, format):
# by omiting request we force all urls to be relative
return super(RelativeHyperlinkedIdentityField, self).get_url(obj, view_name, None, format)

class RelativeHyperlinkedModelSerializer(serializers.HyperlinkedModelSerializer):
_hyperlink_field_class = RelativeHyperlinkedRelatedField
_hyperlink_identify_field_class = RelativeHyperlinkedIdentityField