[Answered ]-Exclude field from a nested serializer

1๐Ÿ‘

โœ…

You can create a new user serializer to use with TeamMemberSerializer.

user serializer

class UserSerializer2(serializers.ModelSerializer):
   class Meta:
      model = User
      fields = ['first_name', 'last_name', 'email', 'company']

team member serializer

class TeamMemberSerializer(serializers.ModelSerializer):
    user = UserSerializer2(read_only=True)
    team = TeamSerializer(read_only=True)
    team_role = TeamRoleSerializer(read_only=True)
    company = CompanySerializer(read_only=True)

    class Meta:
        model = TeamMember
        fields = "__all__"
        read_only_fields = ['state', 'created_at', 'updated_at']
        required_fields = ['team', 'user', 'team_role']
๐Ÿ‘คAlain Bianchini

0๐Ÿ‘

I think you can set the exclude attribute in the UserSerializer.

class UserSerializer(serializers.ModelSerializer):
    ...

    class Meta:
        ...
        exclude = ('password',)
    ...
๐Ÿ‘คMetalgear

Leave a comment