[Django]-Django rest framework – self.context doesn't have request attribute

70👍

How do you create serializer in your viewset’s list() method? You should call

serializer = self.get_serializer(data=request.data)

to get your serializer context filled automatically as it is done in default implementation of this method in DRF mixins., but I have a feeling that you’re just creating it manually, like this:

serializer = MyModelSerializer(instance)

So, to fix this, you should either call get_serializer(), or pass extra context argument to serializer constructor:

serializer = MyModelSerializer(instance, context={'request': request, ...})

Leave a comment