问题描述
我正在尝试将Django rest-api与我在mlab.com上创建的mongo数据库连接.以下是我在Django rest-api的settings.py文件中定义的代码.
I'm trying to connect Django rest-api with mongo database which i created on mlab.com. Below is my code which I define in settings.py file in my Django rest-api.
MONGODB_DATABASES = {
'default': {
'NAME': 'dummy',
'HOST': os.environ.get('MONGO_HOST',
'mongodb://dummyuser:[email protected]:25851/dummy'),
}
}
mongoengine.connection(
db='dummy',
host=os.environ.get('MONGO_HOST',
'mongodb://dummyuser:[email protected]:25851/dummy'),
)
运行此api时出现此错误
When I run this api I got this error
我尝试在线搜索解决方案,但发现了一些针对较旧版本的示例.我使用的是Djangorestframework2.0.7,MongoDB3.4和mongoengine0.15.我找不到此版本的任何答案.我试图将此api连接到本地数据库,但遇到了同样的错误.我该怎么解决?
I tried to search for solutions online but I found examples which were for older versions. I'm using Djangorestframework2.0.7, MongoDB3.4 and mongoengine0.15. I couldn't find any answer for this versions. I tried to connect this api to the local database and I got same error. How can I solve it?
推荐答案
我已成功将django rest-api与mongodb连接.这是对我有用的解决方案.
I have been successfully connected django rest-api with mongodb. Here is the solution that works for me.
数据库= { '默认': { 'ENGINE':'djongo', 'NAME':'dummy', 'HOST':'localhost', }}
DATABASES = { 'default': { 'ENGINE': 'djongo', 'NAME': 'dummy', 'HOST': 'localhost', }}
MONGODB_DATABASES = { 'db':'虚拟', '主机':'本地主机', '端口':27017,}
MONGODB_DATABASES = { 'db': 'dummy', 'host': 'localhost', 'port': 27017,}
此处是更多信息的链接. http://blog.tomjohnhall.com/python-3-6-django-2-0-and-mongodb-3-4-3-6/
Here is the link for more information.http://blog.tomjohnhall.com/python-3-6-django-2-0-and-mongodb-3-4-3-6/
这篇关于如何将Django Rest-api与MongoDB连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!