gpt4 book ai didi

django-rest-framework - Django REST : Exception Value: . filter_queryset() 必须被覆盖

转载 作者:行者123 更新时间:2023-12-04 00:17:46 27 4
gpt4 key购买 nike

我尝试为我的代码库创建一个新安装,但遇到了这个问题。我尽可能地尝试验证两个安装是否具有完全相同的所有依赖项版本。

问题似乎只发生在访问数据库的端点上,尽管这可能只是巧合,问题与数据库无关。 (数据库肯定在运行,因为旧安装仍然可以访问它。)

如果我可以收集任何其他有用的信息来帮助调试,请告诉我。

Traceback:
File "venv/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "venv/lib/python2.7/site-packages/django/views/decorators/csrf.py" in wrapped_view
58. return view_func(*args, **kwargs)
File "venv/lib/python2.7/site-packages/rest_framework/viewsets.py" in view
87. return self.dispatch(request, *args, **kwargs)
File "venv/lib/python2.7/site-packages/rest_framework/views.py" in dispatch
466. response = self.handle_exception(exc)
File "venv/lib/python2.7/site-packages/rest_framework/views.py" in dispatch
463. response = handler(request, *args, **kwargs)
File "venv/lib/python2.7/site-packages/rest_framework/mixins.py" in retrieve
56. instance = self.get_object()
File "venv/lib/python2.7/site-packages/rest_framework/generics.py" in get_object
84. queryset = self.filter_queryset(self.get_queryset())
File "venv/lib/python2.7/site-packages/rest_framework/generics.py" in filter_queryset
151. queryset = backend().filter_queryset(self.request, queryset, self)
File "venv/lib/python2.7/site-packages/rest_framework/filters.py" in filter_queryset
31. raise NotImplementedError(".filter_queryset() must be overridden.")

Exception Type: NotImplementedError at /my/endpoint
Exception Value: .filter_queryset() must be overridden.

这是来自 pip list 的我的包列表:

Package                            Version
---------------------------------- ----------
argcomplete 0.8.1
asn1crypto 0.24.0
backports.shutil-get-terminal-size 1.0.0
beautifulsoup4 4.3.2
cffi 1.11.5
cryptography 2.3.1
Cython 0.28.5
decorator 4.3.0
Django 1.8.4
django-cors-headers 1.1.0
django-filter 0.11.0
djangorestframework 3.2.3
djangorestframework-jwt 1.7.1
dxpy 0.161.0
enum34 1.0.4
fusepy 2.0.2
futures 3.0.3
gunicorn 19.3.0
idna 2.0
impyla 0.10.0
ipaddress 1.0.14
ipdb 0.11
ipython 5.8.0
ipython-genutils 0.2.0
kazoo 2.2.1
Markdown 2.6.2
MySQL-python 1.2.5
ndg-httpsclient 0.4.0
pathlib2 2.3.2
pexpect 4.6.0
pickleshare 0.7.4
pip 18.0
prompt-toolkit 1.0.15
psutil 2.1.3
ptyprocess 0.6.0
pyasn1 0.1.8
pycparser 2.14
Pygments 2.2.0
PyJWT 1.4.0
pymssql 2.2.0.dev0
pyodbc 4.0.24
pyOpenSSL 17.5.0
python-dateutil 2.3
python-magic 0.4.6
requests 2.7.0
scandir 1.9.0
setuptools 40.0.0
simplegeneric 0.8.1
six 1.9.0
SQLAlchemy 1.2.11
thrift 0.9.2
traitlets 4.3.2
wcwidth 0.1.7
WebHDFS 0.2.0
wheel 0.24.0
ws4py 0.3.2
xattr 0.6.4

这是我的休息框架设置。我开始认为使用 BaseFilterBackend 是问题的开始。似乎该类包含未被覆盖的 filter_queryset() 方法:

REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticated',
),
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.SessionAuthentication',
'rest_framework.authentication.BasicAuthentication',
'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
),
'DEFAULT_FILTER_BACKENDS': ('rest_framework.filters.BaseFilterBackend',),
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination',
}

编辑:这是导致错误的 View :

 from rest_framework import permissions
from rest_framework import renderers
from rest_framework import viewsets, mixins
from models import MyModel

from rest_framework.response import Response

from rest_framework.renderers import JSONRenderer
from rest_framework.parsers import JSONParser

from rest_framework import generics
from rest_framework import filters

import six

class MyViewSet(viewsets.ModelViewSet):

queryset = MyModel.objects.all()
serializer_class = MyModelSerializer

#Allows to filter using URL params and sorting
filter_backends = (filters.BaseFilterBackend, filters.OrderingFilter)
ordering = ('CreatedAt',)
filter_fields = ('Modelfield1', 'Modelfield2',)

最佳答案

在您的设置中,您已将 DEFAULT_FILTER_BACKENDS' 设置为 ('rest_framework.filters.BaseFilterBackend',)。因此,BaseFilterBackend 将应用于您的大多数 View *并抛出 NotImplementedError,因为 BaseFilterBackend 是一个实际上并不打算应用的基类直接 - 只会被您覆盖或被其他过滤器继承。您应该将其从您的默认设置以及您正在使用它的任何其他地方删除。

*除了像您在示例 View 中那样通过 filter_backends 手动指定其过滤器的过滤器。

例如,可以通过在设置和 View 中将 BaseFilterBackend 替换为 DjangoFilterBackend 来解决该错误。

关于django-rest-framework - Django REST : Exception Value: . filter_queryset() 必须被覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52560898/

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