我的基于DRF的API引发了一个我似乎无法解决的错误,因为它没有表明来自我的代码的错误。
Traceback (most recent call last):
File "/opt/venv/drf_app/lib/python3.5/site-packages/django/core/handlers/exception.py", line 39, in inner
response = get_response(request)
File "/opt/venv/drf_app/lib/python3.5/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/opt/venv/drf_app/lib/python3.5/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/opt/venv/drf_app/lib/python3.5/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
return view_func(*args, **kwargs)
File "/opt/venv/drf_app/lib/python3.5/site-packages/rest_framework/viewsets.py", line 83, in view
return self.dispatch(request, *args, **kwargs)
File "/opt/venv/drf_app/lib/python3.5/site-packages/rest_framework/views.py", line 477, in dispatch
response = self.handle_exception(exc)
File "/opt/venv/drf_app/lib/python3.5/site-packages/rest_framework/views.py", line 437, in handle_exception
self.raise_uncaught_exception(exc)
File "/opt/venv/drf_app/lib/python3.5/site-packages/rest_framework/views.py", line 448, in raise_uncaught_exception
raise exc
File "/opt/venv/drf_app/lib/python3.5/site-packages/rest_framework/views.py", line 474, in dispatch
response = handler(request, *args, **kwargs)
File "/opt/venv/drf_app/lib/python3.5/site-packages/rest_framework/mixins.py", line 40, in list
queryset = self.filter_queryset(self.get_queryset())
File "/opt/venv/drf_app/lib/python3.5/site-packages/rest_framework/generics.py", line 151, in filter_queryset
queryset = backend().filter_queryset(self.request, queryset, self)
File "/opt/venv/drf_app/lib/python3.5/site-packages/django_filters/rest_framework/filterset.py", line 28, in __init__
super(FilterSet, self).__init__(*args, **kwargs)
File "/opt/venv/drf_app/lib/python3.5/site-packages/django_filters/filterset.py", line 284, in __init__
queryset = self._meta.model._default_manager.all()
AttributeError: 'NoneType' object has no attribute '_default_manager'
其余框架代码中的错误是来自其他地方吗?
仅供参考,django管理员工作正常。
以下是使用的模型和序列化程序:
class ListCreateRetrieveUpdateViewSet(mixins.ListModelMixin, mixins.CreateModelMixin,
mixins.RetrieveModelMixin, mixins.UpdateModelMixin,
viewsets.GenericViewSet):
"""
A viewset that provides `list`, `create`, `retrieve`, `update` actions.
"""
pass
class BaseModel(models.Model):
"""
An abstract base class model that provides self updating
``created`` and ``modified`` fields.
"""
created_on = models.DateTimeField(auto_now_add=True, editable=False)
created_by = models.ForeignKey(settings.AUTH_USER_MODEL,
related_name="%(app_label)s_%(class)s_created_by", editable=False)
modified_on = models.DateTimeField(auto_now=True, editable=False)
modified_by = models.ForeignKey(settings.AUTH_USER_MODEL,
related_name="%(app_label)s_%(class)s_modified_by", editable=False)
is_deleted = models.BooleanField(default=False, verbose_name='Delete Entry')
deleted_on = models.DateTimeField(null=True, blank=True, editable=False)
deleted_by = models.ForeignKey(settings.AUTH_USER_MODEL, related_name="%(app_label)s_%(class)s_deleted_by",
null=True, blank=True, editable=False)
class Meta:
abstract = True
class Country(BaseModel):
"""
List of Countries
"""
code = models.CharField(max_length=3, unique=True)
title = models.CharField(max_length=100, unique=True)
def __str__(self):
return '{} - {}'.format(self.code, self.title)
class CountrySerializer(serializers.ModelSerializer):
"""Serialize model"""
class Meta:
model = Country
class CountryViewSet(viewsets.ReadOnlyModelViewSet):
"""
list and create countries
"""
end_point_name = 'CoreApp'
queryset = Country.objects.all()
serializer_class = CountrySerializer
filter_fields = ('id',)
答案 0 :(得分:1)
我今天遇到了类似的问题:似乎自DRF 3.5起,filters classes have completely moved to django-filter package。我修复了我的导入语句,一切顺利。希望它能帮到你!
django-filter软件包现在完全包含了内置DjangoFilterBackend的功能。
您应该将导入和REST框架过滤器设置更改为 如下:
rest_framework.filters.DjangoFilterBackend
变为django_filters.rest_framework.DjangoFilterBackend
。rest_framework.filters.FilterSet
变为django_filters.rest_framework.FilterSet
。现有导入将继续有效,但现在正在等待弃用。
在我的情况下,最后一句话是假的,它没有用。