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

浪尽此生 提交于 2019-12-01 03:18:27

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, ...})
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!