DRF-路由其他HTTP方法以执行其他操作

时间:2018-10-22 18:28:05

标签: django django-rest-framework

我正在尝试使用django rest框架实现routing additional HTTP methods for extra actions,但在runserver遇到错误

AttributeError: 'function' object has no attribute 'mapping'

无法弄清问题是什么-Django 2.1.2,Python 3.6.5

class CustomerAPIViewSet(mixins.ListModelMixin,
                         mixins.CreateModelMixin,
                         mixins.RetrieveModelMixin,
                         mixins.UpdateModelMixin,
                         viewsets.GenericViewSet):
    permission_classes = (IsAuthenticated, CustomerAdminPermissions)
    serializer_class = CustomerSerializer
    queryset = Customer.objects.all()

    @action(detail=True)
    def order_guide(self, request, **kwargs):
        instance = self.get_object()
        serializer = CustomerDetailSerializer(instance)
        return Response(serializer.data)

    @order_guide.mapping.put
    def order_create(self, request, **kwargs):
        instance = self.get_object()
        serializer = CustomerDetailSerializer(instance, data=request.data)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response(serializer.data)

1 个答案:

答案 0 :(得分:2)

在Django Rest Framework版本3.9.0中引入了用于路由其他HTTP方法的@action.mapping。较早的版本带有AttributeError。尝试升级您的DRF版本。

pip install djangorestframework --upgrade