I would like to hide from public api simple Serializer field (created without any model). How I can solve this?
same_result = serializers.SerializerMethodField()
CodePudding user response:
You can use write_only argument to achieve this goal :
class YourSerializer(serializers.Serializer):
same_result = serializers.SerializerMethodField(write_only=True)
CodePudding user response:
you can override the to to_representation method
def to_representation(self, instance):
data = super().to_representation(instance)
data.pop('same_result')
return data
CodePudding user response:
I think you could either use the permissions configuration to identify either a public or private user based on membership of a group, then you could switch in the View, something like:
class UserViewSet(viewsets.ViewSet):
def list(self, request):
queryset = User.objects.all()
if request.user.groups.filter(name="private").exists():
serializer = PrivateUserSerializer(queryset, many=True)
else:
serializer = PublicUserSerializer(queryset, many=True)
return Response(serializer.data)
Then you define the fields on your serializer as you wish.