I was facing a problem very similar to question 310732 and following the advice there, I implemented the .format() solution in my kwargs generation.
A dump of a small example looks like this:
{'username_fragment_1__startswith': u'joey'}
where "username_fragment_1" is a valid column name.
More details per Daniel's request:
query_sub = "(CASE WHEN ISNULL(%s) THEN 0 ELSE LENGTH(%s) END)"
query_string = query_sub % ((label_dict[frag],)*2)
results[frag] = DisplayNameSearchResult.objects.filter(**kwargs).extra(select={ 'proxylen':(query_string)},order_by=['proxylen']).values('agent_id', 'proxylen')
However, when I run the unit tests, I get:
OperationalError: (1054, "Unknown column 'username_fragment_1__startswith' in 'field list'")
The call in views.py looks something like:
MyClass.objects.filter(**kwargs)
Suggestions appreciated.