[Answer]-Restricted set of nested fields in a django REST framework ModelSerializer

1👍

You can do this by creating your own serializer to use as the nested serializer.

class AccountSerializer(serializers.ModelSerializer):

    class Meta:
        model = Account
        fields = ('name', 'email', )

You are better off with creating specialized serializers instead of relying on Django REST Framework to create them for you. By default, serializers that are automatically created contain all fields defined on the model.

class MyModelSerializer(serializers.ModelSerializer):
    account = AccountSerializer()

    class Meta:
        model = MyModel
        fields = ('id', 'account', )

You can find out more about nested serializers in the Django REST Framework documentation.

Leave a comment