gpt4 book ai didi

django - DRF - 为额外操作路由额外的 HTTP 方法

转载 作者:行者123 更新时间:2023-12-01 13:17:54 26 4
gpt4 key购买 nike

我正在尝试实现 routing additional HTTP methods for extra actions使用 django rest 框架,但在 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)

最佳答案

Django Rest Framework 版本 3.9.0 中引入了用于路由附加 HTTP 方法的 @action.mapping。引发 AttributeError 的早期版本。尝试升级您的 DRF 版本。

pip install djangorestframework --upgrade

关于django - DRF - 为额外操作路由额外的 HTTP 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52935590/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com