简体   繁体   English

在 boolean 字段上使用过滤器的 djongo query_set

[英]djongo query_set with filter on boolean field

I have a django application with djongo as a database driver.我有一个 django 应用程序,将 djongo 作为数据库驱动程序。 My model is simple:我的 model 很简单:

from django.db import models
from djongo.models import ObjectIdField

class TmpModel(models.Model):
    _id = ObjectIdField()
    is_deleted = models.BooleanField(default=False)

When I run in shell simple filter command:当我在 shell 中运行简单的过滤器命令时:

>>> TmpModel().save()   
>>> TmpModel(is_deleted=True).save() 
>>> TmpModel.objects.filter(is_deleted=False).all() 

I got an error:我收到一个错误:

(0.000) QUERY = 'SELECT "solutions_tmpmodel"."_id", "solutions_tmpmodel"."is_deleted" FROM "solutions_tmpmodel" WHERE NOT "solutions_tmpmodel"."is_deleted" LIMIT 21' - PARAMS = (); args=(); alias=default
Traceback (most recent call last):
  File "C:\Users\user\AppData\Local\pypoetry\Cache\virtualenvs\test-task-YJIxAxLW-py3.10\lib\site-packages\djongo\sql2mongo\query.py", line 857, in parse
    return handler(self, statement)
  File "C:\Users\user\AppData\Local\pypoetry\Cache\virtualenvs\test-task-YJIxAxLW-py3.10\lib\site-packages\djongo\sql2mongo\query.py", line 933, in _select
    return SelectQuery(self.db, self.connection_properties, sm, self._params)
  File "C:\Users\user\AppData\Local\pypoetry\Cache\virtualenvs\test-task-YJIxAxLW-py3.10\lib\site-packages\djongo\sql2mongo\query.py", line 116, in __init__
    super().__init__(*args)
  File "C:\Users\user\AppData\Local\pypoetry\Cache\virtualenvs\test-task-YJIxAxLW-py3.10\lib\site-packages\djongo\sql2mongo\query.py", line 62, in __init__
    self.parse()
  File "C:\Users\user\AppData\Local\pypoetry\Cache\virtualenvs\test-task-YJIxAxLW-py3.10\lib\site-packages\djongo\sql2mongo\query.py", line 152, in parse
    self.where = WhereConverter(self, statement)
  File "C:\Users\user\AppData\Local\pypoetry\Cache\virtualenvs\test-task-YJIxAxLW-py3.10\lib\site-packages\djongo\sql2mongo\converters.py", line 27, in __init__
    self.parse()
  File "C:\Users\user\AppData\Local\pypoetry\Cache\virtualenvs\test-task-YJIxAxLW-py3.10\lib\site-packages\djongo\sql2mongo\converters.py", line 119, in parse
    self.op = WhereOp(
  File "C:\Users\user\AppData\Local\pypoetry\Cache\virtualenvs\test-task-YJIxAxLW-py3.10\lib\site-packages\djongo\sql2mongo\operators.py", line 476, in __init__
    self.evaluate()
  File "C:\Users\user\AppData\Local\pypoetry\Cache\virtualenvs\test-task-YJIxAxLW-py3.10\lib\site-packages\djongo\sql2mongo\operators.py", line 465, in evaluate
    op.evaluate()
  File "C:\Users\user\AppData\Local\pypoetry\Cache\virtualenvs\test-task-YJIxAxLW-py3.10\lib\site-packages\djongo\sql2mongo\operators.py", line 258, in evaluate
    self.rhs.negate()
AttributeError: 'NoneType' object has no attribute 'negate'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\user\AppData\Local\pypoetry\Cache\virtualenvs\test-task-YJIxAxLW-py3.10\lib\site-packages\djongo\cursor.py", line 51, in execute
    self.result = Query(
  File "C:\Users\user\AppData\Local\pypoetry\Cache\virtualenvs\test-task-YJIxAxLW-py3.10\lib\site-packages\djongo\sql2mongo\query.py", line 784, in __init__
    self._query = self.parse()
  File "C:\Users\user\AppData\Local\pypoetry\Cache\virtualenvs\test-task-YJIxAxLW-py3.10\lib\site-packages\djongo\sql2mongo\query.py", line 885, in parse
    raise exe from e
djongo.exceptions.SQLDecodeError:

        Keyword: None
        Sub SQL: None
        FAILED SQL: SELECT "solutions_tmpmodel"."_id", "solutions_tmpmodel"."is_deleted" FROM "solutions_tmpmodel" WHERE NOT "solutions_tmpmodel"."is_deleted" LIMIT 21
        Params: ()
        Version: 1.3.6

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\user\AppData\Local\pypoetry\Cache\virtualenvs\test-task-YJIxAxLW-py3.10\lib\site-packages\django\db\backends\utils.py", line 89, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\user\AppData\Local\pypoetry\Cache\virtualenvs\test-task-YJIxAxLW-py3.10\lib\site-packages\djongo\cursor.py", line 59, in execute
    raise db_exe from e
djongo.database.DatabaseError

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:\Users\user\AppData\Local\pypoetry\Cache\virtualenvs\test-task-YJIxAxLW-py3.10\lib\site-packages\django\db\models\query.py", line 370, in __repr__
    data = list(self[: REPR_OUTPUT_SIZE + 1])
  File "C:\Users\user\AppData\Local\pypoetry\Cache\virtualenvs\test-task-YJIxAxLW-py3.10\lib\site-packages\django\db\models\query.py", line 394, in __iter__
    self._fetch_all()
  File "C:\Users\user\AppData\Local\pypoetry\Cache\virtualenvs\test-task-YJIxAxLW-py3.10\lib\site-packages\django\db\models\query.py", line 1866, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "C:\Users\user\AppData\Local\pypoetry\Cache\virtualenvs\test-task-YJIxAxLW-py3.10\lib\site-packages\django\db\models\query.py", line 87, in __iter__
    results = compiler.execute_sql(
  File "C:\Users\user\AppData\Local\pypoetry\Cache\virtualenvs\test-task-YJIxAxLW-py3.10\lib\site-packages\django\db\models\sql\compiler.py", line 1398, in execute_sql
    cursor.execute(sql, params)
  File "C:\Users\user\AppData\Local\pypoetry\Cache\virtualenvs\test-task-YJIxAxLW-py3.10\lib\site-packages\django\db\backends\utils.py", line 103, in execute
    return super().execute(sql, params)
s.py", line 89, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\user\AppData\Local\pypoetry\Cache\virtualenvs\test-task-YJIxAxLW-py3.10\lib\site-packages\djongo\cursor.py", line 59, in execute
    raise db_exe from e
django.db.utils.DatabaseError

How to find out what is wrong?如何找出问题所在?

Use djongo 's model fields for all fields you have instead of django fieldsdjongo的 model 字段用于您拥有的所有字段,而不是django字段

from djongo import models

class TmpModel(models.Model):
    _id = models.ObjectIdField()
    is_deleted = models.BooleanField(default=False)

It seems like an issue with Djongo's Boolean SQL condition parsing (GitHub issue #562) .这似乎是 Djongo 的Boolean SQL 条件解析的问题(GitHub 问题 #562) For now, the solution is to use __in query:目前,解决方案是使用__in查询:

TmpModel.objects.filter(is_deleted__in=[False]).all() 

This has been fixed in djongo@8587ea7 (as of Dec 2022, there's no planned release ).这已在djongo@8587ea7中修复(截至 2022 年 12 月, 没有计划发布)。

pip install git+https://github.com/doableware/djongo.git@8587ea766e4610da5f31112f7b134699e2d603ee

Workaround for Djongo <= 1.3.6 from doableware/djongo#562 (comment 892486144) : Djongo <= 1.3.6 的解决方法来自doableware/djongo#562(评论 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM