Django 的 mongodb 支持库 djongo 会自动关闭数据库
发布时间:2022-07-05 11:49:21 442
相关标签: # django# 数据库# sql# 数据
我使用MongoDB作为Django的数据库,使用djongo进行操作,它似乎会自动关闭连接,并且无法恢复。例如,我创建了一个简单的视图来显示所有记录,如前所述:
book_res = Book.object.all()
...
return Response(res)
它会在我第一次访问视图的 url 地址时起作用,如果我刷新页面或重定向到相同的 url,系统将崩溃并显示错误消息“关闭后无法使用 mongoclient”,这会导致模棱两可的错误“ django.db.utils.DatabseError"
这是完整的回溯错误,顺便说一下,这个网址在我第一次得到它时会起作用,刷新时会发生这个错误
Traceback (most recent call last):
File "C:\Program Files (x86)\Python38\lib\site-packages\djongo\sql2mongo\query.py", line 808, in __iter__
yield from iter(self._query)
File "C:\Program Files (x86)\Python38\lib\site-packages\djongo\sql2mongo\query.py", line 166, in __iter__
for doc in cursor:
File "C:\Program Files (x86)\Python38\lib\site-packages\pymongo\cursor.py", line 1248, in next
if len(self.__data) or self._refresh():
File "C:\Program Files (x86)\Python38\lib\site-packages\pymongo\cursor.py", line 1165, in _refresh
self.__send_message(q)
File "C:\Program Files (x86)\Python38\lib\site-packages\pymongo\cursor.py", line 1052, in __send_message
response = client._run_operation(
File "C:\Program Files (x86)\Python38\lib\site-packages\pymongo\mongo_client.py", line 1267, in _run_operation
return self._retryable_read(
File "C:\Program Files (x86)\Python38\lib\site-packages\pymongo\mongo_client.py", line 1364, in _retryable_read
server = self._select_server(read_pref, session, address=address)
File "C:\Program Files (x86)\Python38\lib\site-packages\pymongo\mongo_client.py", line 1186, in _select_server
topology = self._get_topology()
File "C:\Program Files (x86)\Python38\lib\site-packages\pymongo\mongo_client.py", line 1143, in _get_topology
self._topology.open()
File "C:\Program Files (x86)\Python38\lib\site-packages\pymongo\topology.py", line 188, in open
self._ensure_opened()
File "C:\Program Files (x86)\Python38\lib\site-packages\pymongo\topology.py", line 573, in _ensure_opened
raise InvalidOperation("Cannot use MongoClient after close")
The above exception (Cannot use MongoClient after close) was the direct cause of the following exception:
File "C:\Program Files (x86)\Python38\lib\site-packages\djongo\cursor.py", line 65, in fetchmany
ret.append(self.result.next())
File "C:\Program Files (x86)\Python38\lib\site-packages\djongo\sql2mongo\query.py", line 797, in __next__
result = next(self._result_generator)
File "C:\Program Files (x86)\Python38\lib\site-packages\djongo\sql2mongo\query.py", line 830, in __iter__
raise exe from e
The above exception (
Keyword: FAILED SQL: SELECT "ey_io_compname"."ey_inputName", "ey_io_compname"."ey_outputName" FROM "ey_io_compname" WHERE "ey_io_compname"."ey_inputName" = %(0)s LIMIT 21
Params: ('上海伟仕佳杰科技有限公司',)
Version: 1.3.6
Sub SQL: None
FAILED SQL: None
Params: None
Version: None) was the direct cause of the following exception:
File "C:\Program Files (x86)\Python38\lib\site-packages\django\db\utils.py", line 98, in inner
return func(*args, **kwargs)
File "C:\Program Files (x86)\Python38\lib\site-packages\djongo\cursor.py", line 70, in fetchmany
raise db_exe from e
The above exception () was the direct cause of the following exception:
File "C:\Program Files (x86)\Python38\lib\site-packages\django\core\handlers\exception.py", line 55, in inner
response = get_response(request)
File "C:\Program Files (x86)\Python38\lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Program Files (x86)\Python38\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "C:\Program Files (x86)\Python38\lib\site-packages\django\views\generic\base.py", line 84, in view
return self.dispatch(request, *args, **kwargs)
File "C:\Program Files (x86)\Python38\lib\site-packages\rest_framework\views.py", line 509, in dispatch
response = self.handle_exception(exc)
File "C:\Program Files (x86)\Python38\lib\site-packages\rest_framework\views.py", line 469, in handle_exception
self.raise_uncaught_exception(exc)
File "C:\Program Files (x86)\Python38\lib\site-packages\rest_framework\views.py", line 480, in raise_uncaught_exception
raise exc
File "C:\Program Files (x86)\Python38\lib\site-packages\rest_framework\views.py", line 506, in dispatch
response = handler(request, *args, **kwargs)
File "C:\Trigger\project\EBISre\apis\views.py", line 62, in get
print(data_ormio)
File "C:\Program Files (x86)\Python38\lib\site-packages\django\db\models\query.py", line 296, in __repr__
data = list(self[: REPR_OUTPUT_SIZE + 1])
File "C:\Program Files (x86)\Python38\lib\site-packages\django\db\models\query.py", line 302, in __len__
self._fetch_all()
File "C:\Program Files (x86)\Python38\lib\site-packages\django\db\models\query.py", line 1507, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "C:\Program Files (x86)\Python38\lib\site-packages\django\db\models\query.py", line 130, in __iter__
for row in compiler.results_iter(
File "C:\Program Files (x86)\Python38\lib\site-packages\django\db\models\sql\compiler.py", line 1312, in results_iter
results = self.execute_sql(
File "C:\Program Files (x86)\Python38\lib\site-packages\django\db\models\sql\compiler.py", line 1394, in execute_sql
return list(result)
File "C:\Program Files (x86)\Python38\lib\site-packages\django\db\models\sql\compiler.py", line 1873, in cursor_iter
for rows in iter((lambda: cursor.fetchmany(itersize)), sentinel):
File "C:\Program Files (x86)\Python38\lib\site-packages\django\db\models\sql\compiler.py", line 1873, in
for rows in iter((lambda: cursor.fetchmany(itersize)), sentinel):
File "C:\Program Files (x86)\Python38\lib\site-packages\django\db\utils.py", line 98, in inner
return func(*args, **kwargs)
File "C:\Program Files (x86)\Python38\lib\site-packages\django\db\utils.py", line 91, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:\Program Files (x86)\Python38\lib\site-packages\django\db\utils.py", line 98, in inner
return func(*args, **kwargs)
File "C:\Program Files (x86)\Python38\lib\site-packages\djongo\cursor.py", line 70, in fetchmany
raise db_exe from e
Exception Type: DatabaseError at /api/verifyCompanyRes/
Exception Value:
特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报