从rest_framework.filters导入SearchFilter生成错误,因为无法从“django.db”导入名称“ORDER_PATTERN”

iovurdzv  于 2023-02-05  发布在  Go
关注(0)|答案(2)|浏览(209)

我发现from rest_framework.filters import SearchFilter行生成了一个错误,即

from django.db.models.sql.constants import ORDER_PATTERN
ImportError: cannot import name 'ORDER_PATTERN' from 'django.db.models.sql.constants' (E:\anaconda\envs\AHG_web\lib\site-packages\django\db\models\sql\constants.py)

我用的是如下:

class op_ViewSet(viewsets.ModelViewSet) :
    # permission_classes = (permissions.IsAuthenticated,)

    queryset = Op.objects.all().filter()
    serializer_class = op_Serializer
    # authentication_classes = [TokenAuthentication , SessionAuthentication , BasicAuthentication]
    # pagination_class = PageNumberPagination
    # pagination_class = StandardResultsSetPagination
    filter_backends = [DjangoFilterBackend , SearchFilter]
    filter_class = op_filter
    ordering_fields = ['close_date' , ]
    ordering = ['close_date']
    search_fields = ['website' , 'title' , 'description' , 'organization__name']

    @action(detail=True , methods=['Get'])
    def attachments(self , request , pk) :
        op = self.get_object()
        links = Attachments.objects.filter(op_id=op.id)
        serializer = attachments_Serializer(links , many=True)
        return Response(serializer.data)

以前它工作得很好,有没有人能帮我解决这个问题。

dly7yett

dly7yett1#

这看起来像是Django和Django Rest Framework版本之间的兼容性问题。升级到最新版本的DRF应该可以解决这个问题。
(我在升级Django 3.0.x到3.2后遇到了这个错误)

3pmvbmvn

3pmvbmvn2#

更新已安装在项目中的所有包

pip install <package_name> --upgrade

相关问题