3

I need to search for some data in the database using django ORM and this should be searching by all columns. The problem is that I do not know what to search before I get into the function as a parameter.

This is the source code:

search = request.GET.get('search_value')
#row_to_search is turple i get to function
#it could be different at every searching
#it looks like here:
rows_to_search = ('name', 'age', 'salary')

I need to search in my database for that rows, something like this:

query = Mymodel.objects.filter(Q(name__iregex=search) |      \
                               Q(age__iregex=search)  |      \
                               Q(salary__iregex=search))

How do I make the code if I will get more rows to search for?

I tried something like this but it seams it is not working at all:

Q_filter = map(lambda q: 'Q('+q+')', map(lambda s: s+'__regex='+search, rows_to_search))
searchline = '|'.join(Q_filter)

I can not call model.objects.filter(searchline), because it is an String. Is it possible to do something here, or it is the architecture wrong? Should I make a custom model manager?

0

1 Answer 1

3

Here is one way to do it explicitly:

>>> Q_filter = Q()
>>> for value in rows_to_search:
...     Q_filter |= Q(**{"{}__iregex".format(value): search})

Here is one-liner using reduce():

>>> Q_filter = reduce(lambda x, y: x | Q(**{"{}__iregex".format(y): search}), rows_to_search, Q())

Another one-liner using operator.or_ with list comprehensions:

>>> Q_filter = reduce(operator.or_, [Q(**{"{}__iregex".format(key): search}) for key in rows_to_search])

Then you can call the query as follows:

MyModel.objects.filter(Q_filter)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much ozgul It works perfectly for me!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.