Home > database >  Django Rest Framework fail on setting a new context to the serializer
Django Rest Framework fail on setting a new context to the serializer

Time:01-23

Django time:

I am facing an issue with providing a context to the serializer:

class CommentSerializer(serializers.ModelSerializer):
    likes = CustomUserSerializer(many=True,source='likes.all')



    
    class Meta:
        fields = 'likes',
        model = models.Comment
    
    def get_user_like(self,obj):
        for i in obj.likes.all():
            if self.context['user'] in i.values():

                return self.context['user']

in the view:

class CommentView(viewsets.ModelViewSet):
    serializer_class = serializer.CommentSerializer

    def get_serializer_context(self): #adding request.user as an extra context
        context = super(CommentView,self).get_serializer_context()
        context.update({'user':self.request.user})
        return context

as you can see, i have overridded get_serializer_context to add user as a context

however, in the serializer side, i am getting KeyError:'user' means the key does not exist, any idea how to set a context?

CodePudding user response:

I think that error is in your serializer

def get_user_like(self,obj):
    for i in obj.likes.all():
        if self.context['user'] in i.values():

            return self.context['user']

        return self.context['user']

Try this

def get_user_like(self,obj):
    return self.context['user']

Example

    # from views.py
    def get_serializer_context(self):
        return {'user': self.request.user}
    #in serializers.py
    class ProductSerializer(serializers.ModelSerializer):
            [...]
        
            def create(self, validated_data):
                user = self.context['user']

This is working fine for me. If not working, check what you send from views and receive in the serializer using a debugger.

CodePudding user response:

This is not necessary and inefficient. You can just annotate with:

from django.db.models import Exists, OuterRef


class CommentView(viewsets.ModelViewSet):
    serializer_class = serializer.CommentSerializer

    def get_queryset(self):
        return Comment.objects.annotate(
            user_like=Exists(
                Like.objects.filter(
                    comment_id=OuterRef('pk'), user_id=self.request.user.pk
                )
            )
        ).prefetch_related('likes')

In the serializer we then add the user_like field:

class CommentSerializer(serializers.ModelSerializer):
    likes = CustomUserSerializer(many=True)
    user_like = serializers.BooleanField(read_only=True)

    class Meta:
        fields = ('likes',)
        model = models.Comment
  • Related