Django 查询使用 and 子句
如何在 Django 中使用 and
子句
例如:
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 is a char字段,我构建的查询是否错误,如果是的话,应该如何解决
How to use and
clause in Django
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
ipaddress is a char field,Am i constructing the query wrong if so how this should be resolved
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
!=
在 Django 中的使用不正确(或者实际上,在 Python 中也不正确)。为了排除ipaddress
与特定值匹配的所有记录,您应该使用exclude()
机制。The use of
!=
is not correct in Django (or indeed, not in Python). In order to exclude all records withipaddress
matching a certain value you should use theexclude()
mechanism.这与
and
子句无关。问题在于ipaddress !="192.168.2.211"
是 Django 中的无效过滤器表达式。您只能在过滤器表达式中使用等号,因为它们实际上是过滤器方法的关键字参数。对于您的情况,您可能需要执行以下操作:
This has nothing to do with the
and
clause. The issue is thatipaddress !="192.168.2.211"
is an invalid filter expression in 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: