如何在django中的对象中指定值:is []不等于None? [英] how to specify values in objects in django: is [] not equal to None?

查看:180
本文介绍了如何在django中的对象中指定值:is []不等于None?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果指定或确定了python中对象的值,如果该值为 [] 或者 [<预订:阿拉伯>]
让我们说在我的查询中,我过滤一个不存在于我的对象的值。

how can i specify or determine the values in my objects in python if it has this value [] or it has [<Booking: arab>], let us say that in my queryset, i filter a value that not exist in my objects.

here是我做的:这个date_select没有数据

here is what i have done : this date_select has no data

>>> x = Booking.objects.filter(date_select='2011-12-3')
>>> print x
[]
>>> if x == None:
...  print 'none'
... 
>>> 

是否 [] 不等于 None?
当我尝试一个具有数据的date_select

is it [] not equal to None? when i try a date_select that has data

>>> x = Booking.objects.filter(date_select='2011-12-2')
>>> print x
[<Booking: arab>, <Booking: arab>, <Booking: vvv>]
>>> if x == None:
...  print 'none'
... 
>>>

我只想确定x是否有数据...
可以任何人可以给我一个关于我的情况的想法?

i just want to determine if x has a data or not... can anyone can give me an idea about my case?

提前感谢

推荐答案

p>如果您想知道您的查询结果是否返回任何结果,请致电 存在 方法。

If you want to know if your queryset returned any results, call the exists method.

x = Booking.objects.filter(date_select='2011-12-3')
if x.exists():
   # There are some results from this query set

如果您想知道有多少项目,可以使用 count

If you want to know how many items, you can use count

x = Booking.objects.filter(date_select='2011-12-3')
print x.count()

这篇关于如何在django中的对象中指定值:is []不等于None?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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