gpt4 book ai didi

python - Django 查询集排除正则表达式

转载 作者:行者123 更新时间:2023-12-01 03:58:56 24 4
gpt4 key购买 nike

我有一个过滤请求的命令,我需要按照两条规则提取其中的一些请求。

应该

include '^https?:\/\/[^.]*\.?site\.co([^?]*[?]).*utm_.*$'
or
exclude '^https?:\/\/[^.]*\.?site\.([^\/]+\/)*'

因此,在制定出可能的 SQL 表示形式后,我想出了:

exclude (
matching '^https?:\/\/[^.]*\.?site\.([^\/]+\/)*'
and
not matching '^https?:\/\/[^.]*\.?site\.co([^?]*[?]).*utm_.*$'
)

在 django 中翻译为:

.exclude(
Q(referer__iregex=r'^https?:\/\/[^.]*\.?site\.co([^?]*[?]).*utm_.*$') &
Q(referer__not_iregex=r'^https?://[^.]*\.?site\.[^/]+/?[\?]*$'))

但不幸的是,__not_iregex 查找不存在。有什么解决方法吗?

最佳答案

实际上,您可以对您不想排除的部分使用filter:

queryset
.filter(referer__iregex=r'^https?://[^.]*\.?site\.[^/]+/?[\?]*$')
.exclude(referer__iregex=r'^https?:\/\/[^.]*\.?site\.([^\/]+\/)*')

因此,您的匹配进入排除不匹配进入过滤

或者,如果您确实想模仿 SQL 表示形式中的内容,则可以使用 ~Q:

.exclude(
Q(referer__iregex=r'^https?:\/\/[^.]*\.?site\.([^\/]+\/)*') &
~Q(referer__iregex=r'^https?://[^.]*\.?site\.[^/]+/?[\?]*$'))
# notice use of ~ here

关于python - Django 查询集排除正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36937322/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com