Unfortunately, Django ORM does not have a built-in for reverse LIKE. But a .extra() clause can make this a little easier than a raw request.
I used something like this:
qs.extra( where=['''%s LIKE %s.%s'''], params=( "string to match", FooModel._meta.db_table, "bar_field", ), )
The problems with the code above are that
1) it does not work with the sqlite backend in this form ("syntax error nearby"), it works with table / column names that are hard-coded in the query ... which are not always safe and always ugly);
and 2) FooModel.bar_field requires %in like style% data, so you cannot match arbitrary strings (this can be fixed with a query like %s LIKE CONCAT("%", %s.%s, "%") , but this will make it specific to the DBMS, which is not good).
Reverse LIKE itself should probably work with any large DBMS, but I tested it only on sqlite and postgres.
Perhaps someone should generalize my solution and create a multi-tasking DBMS-agnostic application with a special subclass of queryset / manager / Q-object for this specific task ...
Ivan Anishchuk
source share