2010-09-13 79 views
2

如何使用and子句中的DjangoDjango的查询使用和条款

对于前:

select Date(timestamp) from userlog where (Date(timestamp) >= "2008-01-02" and Date(timestamp) <= "2009-01-02") and ipaddress != "192.168.2.211"; 


    Django query: 

    userlog.objects.filter(timestamp__range=(startdate,enddate),ipaddress !="192.168.2.211") 

在上面有一个错误,说non-keyword arg afterkeyword arg和错误是在上述行

ipaddress是一个字符字段,我是否构建查询错误如果是这样应该如何解决这个问题

回答

3
userlog.objects.filter(timestamp__range=(startdate,enddate),ipaddress !="192.168.2.211") 

使用!=在Django(或实际上,不在Python中)中是不正确的。为了排除与ipaddress匹配的特定值的所有记录,您应该使用exclude()机制。

userlog.objects.filter(timestamp__range=(startdate,enddate)).exclude(ipaddress = 
     "192.168.2.211") 
1

这与and子句无关。问题是ipaddress !="192.168.2.211"在Django中是无效的过滤器表达式。您只能使用在过滤器表达式中使用等号,因为它们实际上是过滤器方法的关键字参数。

在你的情况,你可以需要做的:

userlog.objects.filter(timestamp__range=(startdate,enddate) 
        ).exclude(ipaddress="192.168.2.211")