Django REST@action decorator“未解析的引用'action'”

2024-10-02 18:17:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试在我的views.py中使用@action decorator,但没有被识别。它是不推荐的还是什么

enter image description here

class CategoryTargetGroupDelete(APIView):
    queryset = CategoryTargetGroup.objects.all()

    @action(detail=False, methods=['delete'])
    def delete(self, request):
        cat_id = request.query_params['cat_id']
        queryset = CategoryTargetGroup.objects.filter(category=cat_id)
        queryset.delete()
        return Response(serializer.data)

我已尝试导入操作

enter image description here

DRF 3.7.1

Python 3.9


Tags: pyidobjectsrequestactiondecoratoralldelete
3条回答

我认为您可能没有导入操作

试一试

from rest_framework.decorators import action

@action版本3.8.0中的rest_框架添加了^decorator。您可以增加rest_框架版本或使用@detail_route@list_route代替@action装饰器

更多详情here

如果没有像drf版本这样的附加信息,那就有点难了。 但你能从这里得到行动吗

from rest_framework.decorators import action

相关问题 更多 >