我遇到了可以解决的错误"只需添加到INSTALLED_APPS
(不含migrate
):
'django.contrib.auth',
'django.contrib.contenttypes',
基于调试输出,似乎发生了此错误,因为Django Rest Framework正在尝试对请求进行身份验证,这需要Auth应用程序,该应用程序需要Permissions和ContentTypes,所有这些都已被删除。 APIView仅使用query_params
中的request
,而不是其他内容。它接受全局DRF设置,如下所示:
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (),
'DEFAULT_PERMISSION_CLASSES': (),
'DEFAULT_RENDERER_CLASSES': ('common.rest.JSONRenderer',),
'DEFAULT_CONTENT_NEGOTIATION_CLASS': 'common.rest.IgnoreNegotiation',
}
调试输出:
RuntimeError at /mac/pins/
Model class django.contrib.contenttypes.models.ContentType doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.
Request Method: GET
Request URL: http://django/mac/pins/
Django Version: 1.10.5
Python Executable: /home/admin/env/bin/python3.6
Python Version: 3.6.0
Python Path: ['/home/admin/src', '/home/admin/env/bin', '/home/admin/env/lib/python36.zip', '/home/admin/env/lib/python3.6', '/home/admin/env/lib/python3.6/lib-dynload', '/usr/local/lib/python3.6', '/home/admin/env/lib/python3.6/site-packages']
Server time: Fri, 24 Feb 2017 00:54:33 +0000
Installed Applications:
['myapp']
Installed Middleware:
()
Traceback:
File "/home/admin/env/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
39. response = get_response(request)
File "/home/admin/env/lib/python3.6/site-packages/django/core/handlers/base.py" in _legacy_get_response
249. response = self._get_response(request)
File "/home/admin/env/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "/home/admin/env/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/admin/env/lib/python3.6/site-packages/django/views/decorators/csrf.py" in wrapped_view
58. return view_func(*args, **kwargs)
File "/home/admin/env/lib/python3.6/site-packages/django/views/generic/base.py" in view
68. return self.dispatch(request, *args, **kwargs)
File "/home/admin/env/lib/python3.6/site-packages/rest_framework/views.py" in dispatch
483. response = self.handle_exception(exc)
File "/home/admin/env/lib/python3.6/site-packages/rest_framework/views.py" in handle_exception
443. self.raise_uncaught_exception(exc)
File "/home/admin/env/lib/python3.6/site-packages/rest_framework/views.py" in dispatch
471. self.initial(request, *args, **kwargs)
File "/home/admin/env/lib/python3.6/site-packages/rest_framework/views.py" in initial
393. self.perform_authentication(request)
File "/home/admin/env/lib/python3.6/site-packages/rest_framework/views.py" in perform_authentication
319. request.user
File "/home/admin/env/lib/python3.6/site-packages/rest_framework/request.py" in __getattribute__
379. return super(Request, self).__getattribute__(attr)
File "/home/admin/env/lib/python3.6/site-packages/rest_framework/request.py" in user
196. self._authenticate()
File "/home/admin/env/lib/python3.6/site-packages/rest_framework/request.py" in _authenticate
352. self._not_authenticated()
File "/home/admin/env/lib/python3.6/site-packages/rest_framework/request.py" in _not_authenticated
363. if api_settings.UNAUTHENTICATED_USER:
File "/home/admin/env/lib/python3.6/site-packages/rest_framework/settings.py" in __getattr__
220. val = perform_import(val, attr)
File "/home/admin/env/lib/python3.6/site-packages/rest_framework/settings.py" in perform_import
163. return import_from_string(val, setting_name)
File "/home/admin/env/lib/python3.6/site-packages/rest_framework/settings.py" in import_from_string
177. module = import_module(module_path)
File "/home/admin/env/lib/python3.6/importlib/__init__.py" in import_module
126. return _bootstrap._gcd_import(name[level:], package, level)
File "/home/admin/env/lib/python3.6/site-packages/django/contrib/auth/models.py" in <module>
6. from django.contrib.contenttypes.models import ContentType
File "/home/admin/env/lib/python3.6/site-packages/django/contrib/contenttypes/models.py" in <module>
138. class ContentType(models.Model):
File "/home/admin/env/lib/python3.6/site-packages/django/db/models/base.py" in __new__
113. "INSTALLED_APPS." % (module, name)
Exception Type: RuntimeError at /mac/pins/
Exception Value: Model class django.contrib.contenttypes.models.ContentType doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.
为什么是否尝试对请求进行身份验证?我怎么能阻止它?
答案 0 :(得分:2)
它没有尝试验证请求。你没有任何身份验证器,所以它跳过了一个空元组并转到:
352. self._not_authenticated()
但是,rest框架中未经身份验证的用户对象默认为:
# Authentication
'UNAUTHENTICATED_USER': 'django.contrib.auth.models.AnonymousUser',
因此,如果您不想提取django.contrib.auth
,也许您需要提供自己未经身份验证的用户模型。