Djongo 无法查询 BooleanField

我有一个带有如下模块的 Django 应用程序:

class myApp(models.Model):
    is_new = models.BooleanField(default=True)
    more_fields = models.TextField(blank=True)

并使用 Djongo 作为数据库(即 mongodb)

我可以毫无问题地查询模块中的所有字段,但是,在过滤如下布尔字段时:

    myList = myApp.objects.filter(is_new=False)
        
    for record in myList: ....

失败并出现以下错误:

  File "/home/user/myApp/venv/lib64/python3.9/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 "/home/user/myApp/venv/lib64/python3.9/site-packages/djongo/cursor.py",line 51,in execute
    self.result = Query(
  File "/home/user/myApp/venv/lib64/python3.9/site-packages/djongo/sql2mongo/query.py",line 784,in __init__
    self._query = self.parse()
  File "/home/user/myApp/venv/lib64/python3.9/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 "mysite_myapp"."more_fields","mysite_myapp"."is_new" FROM "mysite_myapp" WHERE NOT "mysite_myapp"."is_new"
        Params: ()
        Version: 1.3.6

除非以不同的方式查询布尔字段,否则这似乎是一个 Djongo 问题

正在使用的版本:

  • Django 3.2.5
  • djongo 1.3.6
sy20031983 回答:Djongo 无法查询 BooleanField

这似乎是 Djongo 的一个老问题(这里是一个最近的 issue,大约一个月前为相同的错误打开),人们被迫使用使用 in 查找的解决方法:

myList = myApp.objects.filter(is_new__in=[False])
本文链接:https://www.f2er.com/2695.html

大家都在问