尝试重置密码时出现 Djongo + Django + MongoDB Atlas DatabaseError
Posted
技术标签:
【中文标题】尝试重置密码时出现 Djongo + Django + MongoDB Atlas DatabaseError【英文标题】:Djongo + Django + MongoDB Atlas DatabaseError when trying to reset password 【发布时间】:2021-03-24 22:34:27 【问题描述】:所以我正在尝试为在 Django 上运行并使用 Djongo 的网站创建密码重置链接。每当我输入密码并单击重置密码按钮时,都会出现此错误:
Environment:
Request Method: POST
Request URL: http://localhost:8000/password-reset/
Django Version: 3.1.4
Python Version: 3.8.3
Installed Applications:
['blog.apps.BlogConfig',
'users.apps.UsersConfig',
'crispy_forms',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django_cleanup']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback (most recent call last):
File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\djongo\cursor.py", line 51, in execute
self.result = Query(
File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\djongo\sql2mongo\query.py", line 783, in __init__
self._query = self.parse()
File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\djongo\sql2mongo\query.py", line 875, in parse
raise e
File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\djongo\sql2mongo\query.py", line 856, in parse
return handler(self, statement)
File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\djongo\sql2mongo\query.py", line 932, in _select
return SelectQuery(self.db, self.connection_properties, sm, self._params)
File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\djongo\sql2mongo\query.py", line 116, in __init__
super().__init__(*args)
File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\djongo\sql2mongo\query.py", line 62, in __init__
self.parse()
File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\djongo\sql2mongo\query.py", line 152, in parse
self.where = WhereConverter(self, statement)
File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\djongo\sql2mongo\converters.py", line 27, in __init__
self.parse()
File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\djongo\sql2mongo\converters.py", line 119, in parse
self.op = WhereOp(
File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\djongo\sql2mongo\operators.py", line 476, in __init__
self.evaluate()
File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\djongo\sql2mongo\operators.py", line 465, in evaluate
op.evaluate()
File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\djongo\sql2mongo\operators.py", line 465, in evaluate
op.evaluate()
File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\djongo\sql2mongo\operators.py", line 279, in evaluate
raise SQLDecodeError
The above exception (
Keyword: None
Sub SQL: None
FAILED SQL: ('SELECT "auth_user"."id", "auth_user"."password", "auth_user"."last_login", "auth_user"."is_superuser", "auth_user"."username", "auth_user"."first_name", "auth_user"."last_name", "auth_user"."email", "auth_user"."is_staff", "auth_user"."is_active", "auth_user"."date_joined" FROM "auth_user" WHERE ("auth_user"."email" iLIKE %(0)s AND "auth_user"."is_active")',)
Params: (('harrynguyen0918@gmail.com',),)
Version: 1.3.3) was the direct cause of the following exception:
File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\djongo\cursor.py", line 59, in execute
raise db_exe from e
The above exception () was the direct cause of the following exception:
File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\django\core\handlers\base.py", line 179, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\django\views\generic\base.py", line 70, in view
return self.dispatch(request, *args, **kwargs)
File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\django\utils\decorators.py", line 43, in _wrapper
return bound_method(*args, **kwargs)
File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\django\utils\decorators.py", line 130, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\django\contrib\auth\views.py", line 222, in dispatch
return super().dispatch(*args, **kwargs)
File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\django\views\generic\base.py", line 98, in dispatch
return handler(request, *args, **kwargs)
File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\django\views\generic\edit.py", line 142, in post
return self.form_valid(form)
File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\django\contrib\auth\views.py", line 235, in form_valid
form.save(**opts)
File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\django\contrib\auth\forms.py", line 311, in save
for user in self.get_users(email):
File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\django\contrib\auth\forms.py", line 287, in get_users
return (
File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\django\db\models\query.py", line 287, in __iter__
self._fetch_all()
File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\django\db\models\query.py", line 1308, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\django\db\models\query.py", line 53, in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\django\db\models\sql\compiler.py", line 1156, in execute_sql
cursor.execute(sql, params)
File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\django\db\backends\utils.py", line 98, in execute
return super().execute(sql, params)
File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\django\db\backends\utils.py", line 66, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\django\db\backends\utils.py", line 75, in _execute_with_wrappers
return executor(sql, params, many, context)
File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\django\db\utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\djongo\cursor.py", line 59, in execute
raise db_exe from e
Exception Type: DatabaseError at /password-reset/
Exception Value:
到目前为止,我似乎找不到遇到同样问题的人。
我认为该错误意味着它无法找到我在数据库中输入的电子邮件。但是,当我在 auth_user
组下的 MongoDB Atlas Collection 选项卡中签入时,我有多个条目使用相同的电子邮件,所以我无法弄清楚发生了什么。
有人遇到过这个问题吗?
【问题讨论】:
【参考方案1】:错误表明 Djongo 的 SQL 解析器在搜索谓词中需要 2 个值和它们之间的运算符。它无法处理直接在 Django 的查询构建器中使用的布尔列 - WHERE 语句的突出显示部分:
WHERE ("auth_user"."email" iLIKE %(0)s AND "auth_user"."is_active")',)
https://github.com/nesdis/djongo/issues/543 报告了这个确切的问题
主要问题https://github.com/nesdis/djongo/issues/562#issuecomment-892486144 中提出的一种战术变通方法建议对数据库包装器进行猴子路径:
from djongo.base import DatabaseWrapper
from djongo.operations import DatabaseOperations
class PatchedDatabaseOperations(DatabaseOperations):
def conditional_expression_supported_in_where_clause(self, expression):
return False
DatabaseWrapper.ops_class = PatchedDatabaseOperations
【讨论】:
以上是关于尝试重置密码时出现 Djongo + Django + MongoDB Atlas DatabaseError的主要内容,如果未能解决你的问题,请参考以下文章
尝试在 MariaDB shell 中重置 Root 密码时出现语法错误
尝试使用 Java API 在 Red Hat SSO 7.3 (Keycloak) 上重置用户密码时出现错误 400