Django过滤器无法与布尔字段一起正常工作

时间:2018-10-26 15:26:45

标签: django django-haystack

我有一个非常简单的查询要过滤,但真的无法理解为什么它给我错误的结果。

这是我的代码-

    for s in sqs:
        print(s.is_draft)
    sqs = sqs.filter(is_draft=False)
    print('----------------------')
    for s in sqs:
        print(s,s.is_draft)

我得到的是-

<SearchResult: products.productcreatemodel (pk='82')> False
<SearchResult: products.productcreatemodel (pk='87')> False
<SearchResult: products.productcreatemodel (pk='94')> False
<SearchResult: products.productcreatemodel (pk='99')> False
<SearchResult: products.productcreatemodel (pk='101')> False
<SearchResult: products.productcreatemodel (pk='106')> False
<SearchResult: products.productcreatemodel (pk='118')> False
<SearchResult: products.productcreatemodel (pk='84')> False
<SearchResult: products.productcreatemodel (pk='91')> False
<SearchResult: products.productcreatemodel (pk='96')> False
<SearchResult: products.productcreatemodel (pk='103')> False
<SearchResult: products.productcreatemodel (pk='108')> False
<SearchResult: products.productcreatemodel (pk='110')> False
<SearchResult: products.productcreatemodel (pk='115')> False
<SearchResult: products.productcreatemodel (pk='85')> False
<SearchResult: products.productcreatemodel (pk='92')> False
<SearchResult: products.productcreatemodel (pk='97')> False
<SearchResult: products.productcreatemodel (pk='104')> False
<SearchResult: products.productcreatemodel (pk='111')> False
<SearchResult: products.productcreatemodel (pk='83')> True
<SearchResult: products.productcreatemodel (pk='95')> False
<SearchResult: products.productcreatemodel (pk='102')> False
<SearchResult: products.productcreatemodel (pk='107')> False
<SearchResult: products.productcreatemodel (pk='119')> False
<SearchResult: products.productcreatemodel (pk='93')> False
<SearchResult: products.productcreatemodel (pk='98')> False
<SearchResult: products.productcreatemodel (pk='100')> False
<SearchResult: products.productcreatemodel (pk='105')> False
<SearchResult: products.productcreatemodel (pk='112')> False
----------------------
<SearchResult: products.productcreatemodel (pk='83')> True

它应该向我返回具有is_draft=False的对象的查询集,但是在此您可以清楚地看到它向我返回具有is_draft=True的对象的查询集。

现在的神奇之处在于,当我再次将代码更改为sqs = sqs.filter(is_draft=True)时,结果是一样的!!

这真的可以理解。

0 个答案:

没有答案