没有数据库的Django Rest框架 [英] Django Rest Framework Without Database
问题描述
我正在尝试使用Django Rest Framework设置一个简单的API,问题是我的API没有任何数据库,但是如果没有数据库设置,该框架将无法正常工作。
I'm trying to setup a simple API using Django Rest Framework, the problem is that my API does not have any database but the framework won't work without database setting.
这是 settings.py
中的Django Rest Framework配置:
Here is my Django Rest Framework configuration in settings.py
:
INSTALLED_APPS = [
'provider',
'django_nose',
'rest_framework',
'django.contrib.contenttypes',
]
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': [],
'DEFAULT_PERMISSION_CLASSES': [],
}
我得到的错误是:
ImproperlyConfigured( settings.DATABASES配置不正确。
django.core.exceptions.ImproperlyConfigured:settings.DATABASES是
配置不正确。请提供ENGINE值。有关详细信息,请查看设置
文档。
ImproperlyConfigured("settings.DATABASES is improperly configured. "django.core.exceptions.ImproperlyConfigured: settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details.
是是否有任何不包含 django.contrib.contenttypes
和 django.contrib.auth
的最小设置?
Is there any minimal settings which does not include django.contrib.contenttypes
and django.contrib.auth
?
推荐答案
问题的实际原因是DRF尝试向其中添加 user
属性请求
。在文档中简要提到的机制如下:
The actual cause of the problem is that DRF trys to add a user
attribute to the request
. Briefly mentioned in the documentation, the mechanism is as follows:
How authentication is determined
如果没有任何类通过身份验证,则将 request.user
设置为
django.contrib.auth.models的实例。 AnonymousUser
If no class authenticates, request.user
will be set to an instance of
django.contrib.auth.models.AnonymousUser
因此它需要 django.contrib.auth
应用程序正确运行,因此 django.contrib.auth
需要一个有效的数据库配置才能执行。
So it needed the django.contrib.auth
application to run correctly, consequently django.contrib.auth
requires a working configuration of Database to be able to perform.
此问题的解决方案是将设置 UNAUTHENTICATED_USER
属性设置为 None
。
The solution to this problem is to set the settings UNAUTHENTICATED_USER
property to None
.
更改后的配置如下:
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': [],
'DEFAULT_PERMISSION_CLASSES': [],
'UNAUTHENTICATED_USER': None,
}
这篇关于没有数据库的Django Rest框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!