简体   繁体   English

Django查询使用和子句

[英]django query using and clause

How to use and clause in Django 如何在Django中使用and子句

For ex: 例如:

    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")

In the above there is an error saying non-keyword arg afterkeyword arg and the error is in the above line 上面有一个错误,说non-keyword arg afterkeyword arg并且该错误在上面的行中

ipaddress is a char field,Am i constructing the query wrong if so how this should be resolved ipaddress是一个char字段,如果是这样,我应该构造错误的查询吗

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

The use of != is not correct in Django (or indeed, not in Python). 在Django中使用!=是不正确的(或者实际上在Python中是不正确的)。 In order to exclude all records with ipaddress matching a certain value you should use the exclude() mechanism. 为了排除ipaddress匹配某个值的所有记录,您应该使用exclude()机制。

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

This has nothing to do with the and clause. 这与and子句无关。 The issue is that ipaddress !="192.168.2.211" is an invalid filter expression in Django. 问题是ipaddress !="192.168.2.211"在Django中是无效的过滤器表达式。 You can only use the equals sign in filter expressions, because they are actually keyword arguments to the filter method. 只能在过滤器表达式中使用等号,因为它们实际上是过滤器方法的关键字参数。

In your case, you can need to do: 对于您的情况,您可能需要执行以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM