我正在尝试将 Django 项目连接到 MongoDB 集群,并且在运行时:
py manage.py runserver
或者
py manange.py migrate
它一直显示相同的错误:
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
Exception in thread django-main-thread:
Traceback (most recent call last):
File "C:\Users\marlo\AppData\Local\Programs\Python\Python37-32\lib\threading.py", line 917, in _bootstrap_inner
self.run()
File "C:\Users\marlo\AppData\Local\Programs\Python\Python37-32\lib\threading.py", line 865, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\marlo\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\utils\autoreload.py", line 54, in wrapper
fn(*args, **kwargs)
File "C:\Users\marlo\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\commands\runserver.py", line 120, in inner_run
self.check_migrations()
File "C:\Users\marlo\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\base.py", line 453, in check_migrations
executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
File "C:\Users\marlo\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\migrations\executor.py", line 18, in __init__
self.loader = MigrationLoader(self.connection)
File "C:\Users\marlo\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\migrations\loader.py", line 49, in __init__
self.build_graph()
File "C:\Users\marlo\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\migrations\loader.py", line 212, in build_graph
self.applied_migrations = recorder.applied_migrations()
File "C:\Users\marlo\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\migrations\recorder.py", line 73, in applied_migrations
if self.has_table():
File "C:\Users\marlo\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\migrations\recorder.py", line 56, in has_table
return self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor())
File "C:\Users\marlo\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\base\introspection.py", line 48, in table_names
return get_names(cursor)
File "C:\Users\marlo\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\base\introspection.py", line 43, in get_names
return sorted(ti.name for ti in self.get_table_list(cursor)
File "C:\Users\marlo\AppData\Local\Programs\Python\Python37-32\lib\site-packages\djongo\introspection.py", line 46, in get_table_list
for c in cursor.db_conn.collection_names(False)
File "C:\Users\marlo\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pymongo\database.py", line 715, in collection_names
nameOnly=True, **kws)]
File "C:\Users\marlo\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pymongo\database.py", line 677, in list_collections
**kwargs)
File "C:\Users\marlo\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pymongo\database.py", line 631, in _list_collections
session=tmp_session)["cursor"]
File "C:\Users\marlo\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pymongo\database.py", line 514, in _command
client=self.__client)
File "C:\Users\marlo\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pymongo\pool.py", line 579, in command
unacknowledged=unacknowledged)
File "C:\Users\marlo\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pymongo\network.py", line 150, in command
parse_write_concern_error=parse_write_concern_error)
File "C:\Users\marlo\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pymongo\helpers.py", line 155, in _check_command_response
raise OperationFailure(msg % errmsg, code, response)
pymongo.errors.OperationFailure: unsupported type in filter
我的 setting.py 配置是:
DATABASES = {
'default': {
'ENGINE': 'djongo',
'NAME': 'database_django',
'HOST': 'mongodb+srv://xxxxxx:aaaaaa@isssgeneral-sfm0v.mongodb.net/test?retryWrites=true&w=majority',
'PORT': 27017,
'USERNAME': 'xxxxxx',
'PASSWORD': 'aaaaaa'
}
}
我的python环境如下:
pymongo 3.7.0
Django 2.2.2
djongo 1.2.32
sqlparse 0.2.4
dnspython 1.16.0
我已经尝试过这个建议将 pymongo 降级到 3.5.1 版本的帖子解决方案,该版本已经尝试过但没有奏效。事实上,该项目以前在 pymongo==3.7 版本(我目前在环境中拥有的那个)中工作。
我已经将这个 Django 项目配置为连接到本地服务器并且它可以工作,显然主机名有些不合适。集群配置没有问题(我认为),因为我有一个直接连接到同一个集群的 Jupyter Notebook。
真的不知道我的项目突然停止连接到我的MongoDB集群是怎么回事。任何人都可以帮助我找到解决这个问题的方法吗?
谢谢社区。