1👍
I’m not sure, but may be you can do it with 2 parameters:
param1 = Q(first_name__icontains=search) | Q(last_name__icontains=search))
param2 = {'foo', False}
db_table.objects.filter(param1, **param2)
1👍
You can build up a variable number of arguments and pass them in as follows:
q = Q(first_name__icontains=search) | Q(last_name__icontains=search)
p = Q(first_in_line=True) | Q(last_in_line=True)
args = [q, p]
kwargs = {
'foo': True
'bar': False
}
db_table.objects.filter(*args, **kwargs)
# Equivalent to:
#
# db_table.objects.filter(
# Q(first_name__icontains=search) | Q(last_name__icontains=search),
# Q(first_in_line=True) | Q(last_in_line=True),
# foo=True,
# bar=False
# )
Now you can use whatever logic you want to build up args
and kwargs
and the call to filter()
will always be the same.
- [Answered ]-Django: how do I code methods in abstract class which rely on a manager?
- [Answered ]-Json formatting trouble, when updating or editing my json file
- [Answered ]-Django View – Do an HTTP POST, but do not generate a new page
- [Answered ]-Django view showing error in virtual environment
- [Answered ]-How to filter by foreign key in GenericRelation?
0👍
Here’s a snippet that’s been around for a while that might help: http://djangosnippets.org/snippets/1679/
- [Answered ]-Second static files directory in Django
- [Answered ]-How to make multiple model queries in a class based view(template view)
- [Answered ]-Regex urlconf in django 1.8 won't work
- [Answered ]-Django form fails to render_to_response
Source:stackexchange.com