Django布尔查询过滤器不工作 [英] Django Boolean Queryset Filter Not Working

查看:125
本文介绍了Django布尔查询过滤器不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有以下型号:

  sold = models.BooleanField(default = False)

和以下视图代码:

  properties = Property.objects.filter(sold = False).order_by(' -  created_on') ; 

我的sqlite3数据库中的以下值:

  sqlite>选择从clients_property出售; 
1
1
1
1
1

以下模板代码DOES工作(如,隐藏已售项目):

  {%if not property .sold%} 

任何人都知道为什么查询集过滤器不工作或为什么我在做错了吗我试过:

  sold =1
sold = 1
sold =false
sold = False
sold =False


解决方案

从你发布的内容中,一切正如广告一样工作。如果你从shell尝试这个东西,你应该得到以下结果。当然,我正在做一些它,所以在你刚刚复制粘贴之前,请阅读。

 >>> from myapp.models import Property 
>>> Property.objects.all()
[< Property:Property object>,< Property:Property object>,< Property:Property object>,< Property:Property object>,& ;,]
>>> Property.objects.filter(sold = False)
[]
>>> Property.objects.filter(sold = True)
[< Property:Property object>,< Property:Property object>,< Property:Property object>,< Property:Property object>,& :属性对象>]
>>> Property.objects.create(sold = False,my ='other',fields = 1)
>>> Property.objects.filter(sold = False)
[< Property:Property object>,]



杰克是对的,在大多数SQL实现中,1应该评估为True。


This has been frustrating me for the better part of an hour.

I have the following model:

sold= models.BooleanField(default=False)

And the following view code:

properties = Property.objects.filter(sold=False).order_by('-created_on');

And the following values in my sqlite3 database:

 sqlite> select sold from clients_property;
1
1
1
1
1

And the following template code DOES work (as in, hides the sold items):

{% if not property.sold %}

Anyone know why the query set filter isn't working or why I'm doing it wrong? I've tried:

sold="1"
sold=1
sold="false"
sold=False
sold="False"

解决方案

From what you've posted, everything is working as advertised. If you try this stuff from the shell, you should get the following results. Of course I'm making some of it up, so read before you just copy-paste.

>>> from myapp.models import Property
>>> Property.objects.all()
[<Property: Property object>,<Property: Property object>,<Property: Property object>,<Property: Property object>,<Property: Property object>,]
>>> Property.objects.filter(sold=False)
[]
>>> Property.objects.filter(sold=True)
[<Property: Property object>,<Property: Property object>,<Property: Property object>,<Property: Property object>,<Property: Property object>,]
>>> Property.objects.create(sold=False, my='other', fields=1)
>>> Property.objects.filter(sold=False)
[<Property: Property object>,]

Jack is right, 1 should evaluate to True in most SQL implementations.

这篇关于Django布尔查询过滤器不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆