Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel


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 :

The request object is not available in context by default. In order to access it in TodoSerializer you have to explicitly add context={'request': request} when instantiating the serializer.

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, context={'request': request})
        return Response(serializer.data)

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading