Dimitris Leventeas
Dimitris Leventeas

Reputation: 1702

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 use verbatim the code in the link.

Upvotes: 1

Views: 84

Answers (1)

Issac Kelly
Issac Kelly

Reputation: 6359

I'd guess you're looking for Q objects. Really though, you should try django-haystack, if you're doing anything related to searching.

Upvotes: 1

Related Questions