For example, the query is:
a = request.GET['country']
b = request.GET['name']
c = request.GET['title']
...
result = models.Data.objects.filter('country' = a, 'name' = b, 'title' = c, ...)
What should I do if one of these a b c is "Any"?
I mean if I receive from the frontend, a="Any", how should I effectively free this limit in filter?
You can make a function to build a Q
object from a parameter:
def _get_filter(name, value):
return Q() if value == 'Any' else Q((name, value))
and combine them with &
:
q = (
_get_filter('country', request.GET['country']) &
_get_filter('name', request.GET['name']) &
_get_filter('title', request.GET['title'])
)
result = models.Data.objects.filter(q)
If possible, I’d also get the source of the request (frontend?) to send a better value to indicate “Any”, like an empty string.