[Django]-Django Rest Framework: Add hyperlink related field on sub model

2👍

As source you can make use of model2.model3 to access the object of that ForeignKey:

class Model1ListSerializer(ValidateByModelSerializer):
    id = serializers.IntegerField(read_only=True)
    model3 = serializers.HyperlinkedRelatedField(
        source='model2.model3',
        queryset=Model3.objects.all(),
        view_name='model3_rest-detail'
     )

I would advise to also select the related models in the same query, so use this with:

Model1.objects.select_related('model2', 'model2__model3')

Leave a comment