Reputation: 3076
I try to pass filter parameter from form to view in order to filter objects in Django by custom "index":
profile_list = Profile.objects.filter(
company=request.user.company,
)
if request.method == 'POST':
search_key = request.POST.get('skey')
search_query = request.POST.get('squery', None)
profile_list = profile_list.filter(
search_key=search_query
)
But I can't use this code, I get:
Cannot resolve keyword 'search_key' into field.
Which is normal, there is no search_key attribute. How should I format this to work properly?
Upvotes: 0
Views: 132
Reputation: 118488
If you want the value of the variable search_key
to be used as the keyword, you'll want to throw it in a dict and use keyword expansion via **
profile_list = profile_list.filter(
**{search_key:search_query}
)
profile_list = profile_list.filter(
**{'%s__contains' % search_key:search_query}
)
Upvotes: 1
Reputation: 53366
You can create dict and pass that to filter as
qdict = { search_key: search_query }
profile_list = profile_list.filter( **qdict)
Upvotes: 1