带有布尔字段的 django ORM 过滤器

问题描述 投票:0回答:0

我创建了一个名为 Tag 的模型

class Tag(models.Model):
    name = models.CharField(max_length=255)
    valid = models.BooleanField(default=False)
    
    def __str__(self):
        return self.name

然后在视图中我尝试使用有效字段进行过滤

tags = Tag.objects.filter(valid=False)
print(tags)

查询正常,但在打印输出时抛出异常“django.db.utils.DatabaseError”。 如果我再次尝试

tags = Tag.objects.filter(name="Binary Search")
print(tags)

效果很好。即使我尝试在 auth.User 模型中使用 is_staff 进行过滤,它也显示相同的错误

完全错误

Internal Server Error: /problems/addproblem/
Traceback (most recent call last):
  File "C:\Users\USER\AppData\Local\Programs\Python\Python310\lib\site-packages\djongo\sql2mongo\query.py", line 857, in parse
    return handler(self, statement)
  File "C:\Users\USER\AppData\Local\Programs\Python\Python310\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\Programs\Python\Python310\lib\site-packages\djongo\sql2mongo\query.py", line 116, in __init__
    super().__init__(*args)
  File "C:\Users\USER\AppData\Local\Programs\Python\Python310\lib\site-packages\djongo\sql2mongo\query.py", line 62, in __init__
    self.parse()
  File "C:\Users\USER\AppData\Local\Programs\Python\Python310\lib\site-packages\djongo\sql2mongo\query.py", line 152, in parse
    self.where = WhereConverter(self, statement)    
  File "C:\Users\USER\AppData\Local\Programs\Python\Python310\lib\site-packages\djongo\sql2mongo\converters.py", line 27, in __init__
    self.parse()
  File "C:\Users\USER\AppData\Local\Programs\Python\Python310\lib\site-packages\djongo\sql2mongo\converters.py", line 119, in parse
    self.op = WhereOp(
  File "C:\Users\USER\AppData\Local\Programs\Python\Python310\lib\site-packages\djongo\sql2mongo\operators.py", line 475, in __init__
    self._statement2ops()
  File "C:\Users\USER\AppData\Local\Programs\Python\Python310\lib\site-packages\djongo\sql2mongo\operators.py", line 438, in _statement2ops
    if prev_op.lhs is None:
AttributeError: 'NoneType' object has no attribute 'lhs'

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

Traceback (most recent call last):
  File "C:\Users\USER\AppData\Local\Programs\Python\Python310\lib\site-packages\djongo\cursor.py", line 
51, in execute
    self.result = Query(
  File "C:\Users\USER\AppData\Local\Programs\Python\Python310\lib\site-packages\djongo\sql2mongo\query.py", line 784, in __init__
    self._query = self.parse()
  File "C:\Users\USER\AppData\Local\Programs\Python\Python310\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 "problems_tag"."id", "problems_tag"."name", "problems_tag"."valid" FROM "problems_tag" WHERE "problems_tag"."valid" 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\Programs\Python\Python310\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\USER\AppData\Local\Programs\Python\Python310\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 "C:\Users\USER\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\Users\USER\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\USER\AppData\Local\Programs\Python\Python310\lib\site-packages\django\contrib\auth\decorators.py", line 21, in _wrapped_view
    return view_func(request, *args, **kwargs)      
  File "C:\Users\USER\Documents\random\django_pro\programming_adda\problems\views.py", line 64, in addproblem
    print(tags)
  File "C:\Users\USER\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\query.py", line 256, in __repr__
    data = list(self[:REPR_OUTPUT_SIZE + 1])        
  File "C:\Users\USER\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\query.py", line 280, in __iter__
    self._fetch_all()
  File "C:\Users\USER\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\query.py", line 1324, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "C:\Users\USER\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\query.py", line 51, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)        
  File "C:\Users\USER\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\sql\compiler.py", line 1175, in execute_sql
    cursor.execute(sql, params)
  File "C:\Users\USER\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\utils.py", line 98, in execute
    return super().execute(sql, params)
  File "C:\Users\USER\AppData\Local\Programs\Python\Python310\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\USER\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\utils.py", line 75, in _execute_with_wrappers
    return executor(sql, params, many, context)     
  File "C:\Users\USER\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\utils.py", line 79, in _execute
    with self.db.wrap_database_errors:
  File "C:\Users\USER\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "C:\Users\USER\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\USER\AppData\Local\Programs\Python\Python310\lib\site-packages\djongo\cursor.py", line 
59, in execute
    raise db_exe from e
django.db.utils.DatabaseError

我试过了

tags = Tag.objects.filter(valid=False)
print(tags)

并期望打印对象而不是抛出错误。

django-models django-orm django-filter
© www.soinside.com 2019 - 2024. All rights reserved.