什么是有效的方法来做和/或搜索Django

编程入门 行业动态 更新时间:2024-10-23 21:25:43
本文介绍了什么是有效的方法来做和/或搜索Django-Postgres App?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在我的Django应用程序中,我有一个出版模型和一个标签模型,它们有很多关系。假设我有四个标签:男士,女士,时尚,设计。我想找到所有与其相关联的男性或女性标签的出版物,以及与之相关的时尚或设计标签。例如,在搜索字段中,我可以输入:(men || women)&& (时尚||设计)。换句话说,结果应该产生每个或对中至少一个标签的出版物。

我已经弄清楚如何做一个或搜索(即查找所有具有男性或女性标签或时尚或设计的出版物),并且我已经弄清楚如何做一个搜索(即查找所有标签,男人和女人的所有出版物和时尚和设计),但我不知道最有效的方法来做和组合。

到目前为止,我已经分离了查询字符串以获取或对的列表,我开始创建一个搜索谓词,但后来我想,我做这些结果?例如...

if request中的'qTag'.GET: or_queries = request.GET ['qTag '] .split('&&') $ b $ or在or_queries中的or_query: or_query_set = or_query.split()在or_query_set中的sub_query: if pub_predicate: pub_predicate = pub_predicate | Q(tags__title__istartswith = sub_query) else: pub_predicate = Q(tags__title__istartswith = sub_query) pubs_for_set = Publication.objects.filter(pub_predicate).distinct() pubs_for_tags.append pubs_for_set) pub_predicate =无 pubs_for_set =无

列表(pubs_for_tags)的列表(pubs_for_set),为每个或对提供所有结果。但现在什么?我不知道如何进行和搜索任何人都可以建议一种方式这个?

解决方案

我不知道是否有办法做到这一点,我想说试试把过滤器与Q但是后来又想到了:

在Django查询中组合AND OR

AND OR Django查询II

In my Django app I have a Publication model and a Tag model which have a many to many relationship.

Let's say I have four tags: men, women, fashion, design. I want to find all publications that have a men OR women tag associated with it, AND a fashion OR design tag associated with it. For example, in a search field I might enter: (men || women) && (fashion || design). In other words, the results should yield publications that have at least one tag from each 'or' pair.

I have figured out how to do just an 'or' search (i.e. find all publications with a tag of men or women or fashion or design) and I have figured out how to do just an and search (i.e. find all publications that have all the tags, men and women and fashion and design), but I do not know the most efficient way to do the and-or combo.

Thus far I have split up the query string to get a list of 'or' pairs, and I started to create a search predicate, but then I thought, what will I do with these results? For example...

if 'qTag' in request.GET: or_queries = request.GET['qTag'].split('&&') for or_query in or_queries: or_query_set = or_query.split() for sub_query in or_query_set: if pub_predicate: pub_predicate = pub_predicate | Q(tags__title__istartswith=sub_query) else: pub_predicate = Q(tags__title__istartswith=sub_query) pubs_for_set = Publication.objects.filter(pub_predicate).distinct() pubs_for_tags.append(pubs_for_set) pub_predicate = None pubs_for_set = None

Doing the above gives me a list (pubs_for_tags) of lists (pubs_for_set) that gives all results for each "or"pair." But now what? I don't know how to proceed with an and search. Can anyone suggest a way to do this?

解决方案

I'm not sure if there is a way to do this. I wanted to say try combining filter with Q but then chanced upon these:

Combining AND OR in Django Queries

and

AND OR in Django queries II

更多推荐

什么是有效的方法来做和/或搜索Django

本文发布于:2023-10-23 02:12:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1519434.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:来做   方法   Django

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!