Database queries with OR
I would like to implement a search box which returns all the results which are associated with at least one of the terms. In other words, a query with the terms separated by OR
.
For instance:
Input: tomatoes potatoes watermelons
Query: tomatoes OR potatoes OR watermelons in db
Output: corresponding results
Preferably, but not necessarily, if a term is contained in all the results, it should be first or in the group of firsts.
I looked it up on Google but I could not modify an example that I found. If I replace the last row in get_query
with query = query | or_query
, I get an error in the template. I guess it is because of the lazy evaluation of the queries that Django performs, since the template works if I us开发者_Python百科e verbatim the code in the link.
I'd guess you're looking for Q objects. Really though, you should try django-haystack, if you're doing anything related to searching.
精彩评论