5👍
HyperlinkedModelSerializer
uses a single parameter in the lookup_field
as of DRF 2.0, so like you, I wasn’t able to use this.
However, with some tricks I picked up on this question I was able to build a ModelSerializer
that would have a url
field that contained the reverse URL as you describe.
class OrderSerializer(serializers.ModelSerializer):
# Fields, etc
url = serializers.SerializerMethodField('make_url')
class Meta:
model = Order
# Class info here as usual
def make_url(self, obj):
"""
Build URL for Order instance
"""
# Prepare the IDs you need for the URL reverse
kwargs = {
'customer_pk': obj.customer.id,
'order_id': obj.id,
}
url = reverse('api:single_order', kwargs=kwargs)
return self.context['request'].build_absolute_uri(url)
Source:stackexchange.com