Search code examples
pythondjangodjango-rest-frameworkdrf-queryset

request = self.context.get('request') , give me'NoneType' object has no attribute 'data'


I'm trying to exclude certain fields from being displayed in the list view of my serializer. To achieve this, I have overridden the to_representation method.
However, when I attempt to access the 'request' object using request = self.context.get('request'),
I encountered an error stating "'NoneType' object has no attribute 'data'.

this is serializer.py

from rest_framework import serializers
from todo.models import Task

class TodoSerializer(serializers.ModelSerializer):
    short_content=serializers.ReadOnlyField(source='get_short_content')
    
    author = serializers.CharField(source="author.username", read_only=True)
    class Meta:
        model = Task
        fields =['id','title', 'author' ,'details' ,'short_content','isCompleted','created_date','updated_date']
        read_only_fields=['author']


  
   def to_representation(self,instance):
        request=self.context.get('request')
        rep =super().to_representation(instance)
        print(request.data)
        if request.parser_context.get('kwargs').get('pk'):
            rep.pop('short_content',None)
            
        else:
            rep.pop('details',None)
        return rep

views.py


from rest_framework import viewsets
from rest_framework.response import Response
from rest_framework.permissions import IsAuthenticated
from rest_framework import generics

from todo.models import Task
from .serializers import TodoSerializer



class TaskListViewSets(viewsets.ModelViewSet):

    permission_classes=[IsAuthenticated]
    queryset=Task.objects.all()
    serializer_class=TodoSerializer

    def get_queryset(self):
        return Task.objects.filter(author=self.request.user.id)

    def list(self, request):
        queryset = self.get_queryset().filter(author=request.user)
        serializer = TodoSerializer(queryset, many=True)
        return Response(serializer.data)

    def perform_create(self,serializer):
        serializer.save(author=self.request.user)

Any insights into why I might be receiving this error and how to resolve it would be greatly appreciated. Thank you!


Solution

  • You are implementing too much boilerplate, the list will work with a serializer with the right context:

    from rest_framework import generics, viewsets
    from rest_framework.permissions import IsAuthenticated
    from rest_framework.response import Response
    from todo.models import Task
    
    from .serializers import TodoSerializer
    
    
    class TaskListViewSet(viewsets.ModelViewSet):
    
        permission_classes = [IsAuthenticated]
        queryset = Task.objects.all()
        serializer_class = TodoSerializer
    
        def get_queryset(self):
            return Task.objects.filter(author=self.request.user.id)
    
        # no list method, this is implemented with the right serializer
    
        def perform_create(self, serializer):
            serializer.save(author=self.request.user)