django默认支持的数据库只有 ‘mysql’, ‘oracle’, ‘postgresql’, ‘sqlite3’。
为了连接mongodb可参考文章:借助Djongo模块在Django中使用MongoDB数据库。
在django中使用django
连接mongodb时,修改settings.py
为如下:
DATABASES = {
'default': {
'ENGINE': 'djongo',
'NAME': 'xxx',
}
}
修改后报错:
/Applications/PyCharm.app/Contents/helpers/pycharm/django_manage.py startapp ddauth /Users/zhbink/Documents/GitHub/DandangServer"
/etc/profile: line 11: []: command not found
Tracking file by folder pattern: migrations
Traceback (most recent call last):
File "/Users/zhbink/opt/anaconda3/envs/django/lib/python3.7/site-packages/django/db/utils.py", line 110, in load_backend
return import_module('%s.base' % backend_name)
File "/Users/zhbink/opt/anaconda3/envs/django/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/Users/zhbink/opt/anaconda3/envs/django/lib/python3.7/site-packages/djongo/base.py", line 11, in
from . import database as Database
File " /Users/zhbink/opt/anaconda3/envs/django/lib/python3.7/site-packages/djongo/database.py", line 2, in
from pymongo import MongoClient
File " /Users/zhbink/opt/anaconda3/envs/django/lib/python3.7/site-packages/pymongo/__init__.py", line 77, in
from pymongo.collection import ReturnDocument
File " /Users/zhbink/opt/anaconda3/envs/django/lib/python3.7/site-packages/pymongo/collection.py", line 20, in
from bson.code import Code
File " /Users/zhbink/opt/anaconda3/envs/django/lib/python3.7/site-packages/bson/code.py", line 18, in
from bson.py3compat import abc, string_type, PY3, text_type
ImportError: cannot import name 'abc' from 'bson.py3compat' (/Users/zhbink/opt/anaconda3/envs/django/lib/python3.7/site-packages/bson/py3compat.py)
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File " /Applications/PyCharm.app/Contents/helpers/pycharm/django_manage.py", line 52, in
run_command()
File " /Applications/PyCharm.app/Contents/helpers/pycharm/django_manage.py", line 46, in run_command
run_module(manage_file, None, '__main__', True)
File "/Users/zhbink/opt/anaconda3/envs/django/lib/python3.7/runpy.py", line 205, in run_module
return _run_module_code(code, init_globals, run_name, mod_spec)
File "/Users/zhbink/opt/anaconda3/envs/django/lib/python3.7/runpy.py", line 96, in _run_module_code
mod_name, mod_spec, pkg_name, script_name)
File "/Users/zhbink/opt/anaconda3/envs/django/lib/python3.7/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/Users/zhbink/Documents/GitHub/DandangServer/manage.py", line 21, in
main()
File " /Users/zhbink/Documents/GitHub/DandangServer/manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "/Users/zhbink/opt/anaconda3/envs/django/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/Users/zhbink/opt/anaconda3/envs/django/lib/python3.7/site-packages/django/core/management/__init__.py", line 357, in execute
django.setup()
File "/Users/zhbink/opt/anaconda3/envs/django/lib/python3.7/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/Users/zhbink/opt/anaconda3/envs/django/lib/python3.7/site-packages/django/apps/registry.py", line 114, in populate
app_config.import_models()
File "/Users/zhbink/opt/anaconda3/envs/django/lib/python3.7/site-packages/django/apps/config.py", line 211, in import_models
self.models_module = import_module(models_module_name)
File "/Users/zhbink/opt/anaconda3/envs/django/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/Users/zhbink/opt/anaconda3/envs/django/lib/python3.7/site-packages/django/contrib/auth/models.py", line 2, in
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File " /Users/zhbink/opt/anaconda3/envs/django/lib/python3.7/site-packages/django/contrib/auth/base_user.py", line 47, in
class AbstractBaseUser(models.Model):
File " /Users/zhbink/opt/anaconda3/envs/django/lib/python3.7/site-packages/django/db/models/base.py", line 117, in __new__
new_class.add_to_class('_meta', Options(meta, app_label))
File "/Users/zhbink/opt/anaconda3/envs/django/lib/python3.7/site-packages/django/db/models/base.py", line 321, in add_to_class
value.contribute_to_class(cls, name)
File "/Users/zhbink/opt/anaconda3/envs/django/lib/python3.7/site-packages/django/db/models/options.py", line 204, in contribute_to_class
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
File "/Users/zhbink/opt/anaconda3/envs/django/lib/python3.7/site-packages/django/db/__init__.py", line 28, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "/Users/zhbink/opt/anaconda3/envs/django/lib/python3.7/site-packages/django/db/utils.py", line 201, in __getitem__
backend = load_backend(db['ENGINE'])
File "/Users/zhbink/opt/anaconda3/envs/django/lib/python3.7/site-packages/django/db/utils.py", line 125, in load_backend
) from e_user
django.core.exceptions.ImproperlyConfigured: 'djongo' isn't an available database backend.
Try using 'django.db.backends.XXX', where XXX is one of:
'mysql', 'oracle', 'postgresql', 'sqlite3'
错误原因:
pymongo
与djongo
版本不兼容。
对于djongo1.3版本,pymongo3.9以上不兼容,请将pymongo退回3.7版本:
$ pip uninstall pymongo
$ pip install pymongo==3.7.2
此外,djongo必须配合django2版本使用。
参考:
djongo’ isn’t an available database backend