Py学习  »  Django

QuerySets filter支持匹配任意值吗?

lreij • 11 年前 • 4467 次点击  

比如

某个产品,有品牌、特性1、特性2、特性3,。。。等字段

想要精确查询符合其中某几项的产品,现在用的方法是使用大量的if语句,来分别指定filter函数的参数:

if (brand == 'xxx') and (attr1 == 'xxxx') and (attr2 == 'xxxxx'):
    p = product.objects.filter(...)
if (brand == 'xxx') and (attr1 == 'xxxx'):
    p = product.objects.filter(...)
if (attr1 == 'xxx') and (attr2 == 'xx'):
    p = product.objects.filter(...)

有没有直接的方法用一行来搞定?也就是 attr == ? 时候,filter就不再过滤这个字段了?

product.objects.filter(...)

没搜到相关文档,请指点,谢谢!

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/230
 
4467 次点击  
文章 [ 2 ]  |  最新文章 11 年前