Is there a way to check if a string is a valid filter for a django queryset?
<p>I'm trying to add some functionality to give a user the ability to filter a paginated queryset in Django via URL get parameters, and have got this successfully working:</p>

<pre><code>for f in self.request.GET.getlist('f'):
    try:
        k,v = f.split(':', 1)
        queryset = queryset.filter(**{k:v})
    except:
        pass
</code></pre>

<p>However, I am hoping to do so in a way that doesn't use <code>try/except</code> blocks. Is there a standard way in django to check if a string is a valid filter parameter?</p>

<p>For example something like:</p>

<pre><code>my_str = "bad_string_not_in_database"
if some_queryset.is_valid_filter_string(my_str):
   some_queryset.filter(**{my_str:100}) 
</code></pre>

<p>You can start by looking at the field names:</p>

<pre><code>qs.model._meta.get_all_field_names()
</code></pre>

<p>You are also probably going to want to work with the extensions such as <code>field__icontains</code>, <code>field__gte</code> etc.  So more work will be required.  </p>

<p><em>Disclaimer</em>:  <code>try</code>/<code>except</code> is the far superior way.  I don't know why you want to dismiss this method.  </p>

python django django-queryset django-orm