Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django rest框架,应该能够覆盖get_queryset而不是定义queryset属性?_Python_Django_Django Rest Framework - Fatal编程技术网

Python Django rest框架,应该能够覆盖get_queryset而不是定义queryset属性?

Python Django rest框架,应该能够覆盖get_queryset而不是定义queryset属性?,python,django,django-rest-framework,Python,Django,Django Rest Framework,我很困惑。通过查看ViewSet源代码,看起来我应该能够在ViewSet中不定义queryset,然后重写get queryset函数来获取我想要的任何queryset。但我的代码因以下错误而失败: AssertionError: `base_name` argument not specified, and could not automatically determine the name from the viewset, as it does not have a `.queryset`

我很困惑。通过查看ViewSet源代码,看起来我应该能够在ViewSet中不定义queryset,然后重写get queryset函数来获取我想要的任何queryset。但我的代码因以下错误而失败:

AssertionError: `base_name` argument not specified, and could not automatically determine the name from the viewset, as it does not have a `.queryset` attribute.
因此,即使我覆盖queryset属性,我仍然需要在开始时将其设置为一些伪属性。。。这是可行的,但是定义queryset并在一秒钟后重写它会让人感觉很奇怪

class StudyQuestion(viewsets.ReadOnlyModelViewSet):
    queryset = Model.objects.all()
    serializer_class = ModelSerializer
    permission_classes = (permissions.IsAuthenticated, )

    def get_queryset(self):
        """"""
        return Model.objects.order_by('-word__frequency')

DRF路由器正在抱怨,因为它不能:

base\u name-用于创建的URL名称的基。如果取消设置,则将根据viewset的queryset属性(如果有)自动生成basename。请注意,如果视图集不包含queryset属性,则在注册视图集时必须设置基本名称


DRF模型视图集使用
queryset
派生URL基。如果未设置
queryset
属性,DRF会要求您在注册路由器时使用可选的
base\u name
属性来声明基站

在DRF文档中查看此页面: