[Answered ]-Showing a related fields attribute

2👍

Try something like this.

class NotificationReceiverSerializer(serializers.ModelSerializer):
    proteced_area = serializers.ReadOnlyField(source="protected_area.name")

    class Meta:
        model = NotificationReceiver
        fields = ('pk','cellphone', 'protected_area')

This will show the protected_area names as a read only field. Alternatively,

class NotificationReceiverSerializer(serializers.ModelSerializer):
    proteced_area = ProtectedAreaSerializer(read_only=True, many=True)

    class Meta:
        model = NotificationReceiver
        fields = ('pk','cellphone', 'protected_area')

to show all the fields in the related model

👤e4c5

Leave a comment