In Django, there’s the possibility of specifying field lookups. Those are the ones that enable you to use a syntax like:
my_queryset.filter(id__in=[1, 2, 6])
This would create an SQL query like:
SELECT ... WHERE id IN (1, 2, 6);
In other words: each object is filtered depending on the value (right hand side) of a specified field (left hand side).
I want to do something different. I want to filter each object, not depending on the value of a field, but depending on an arbitrary value instead. More specifically, what I want to do is to use a MATCH
query in SQLite, which uses the following syntax:
SELECT ... WHERE table_name MATCH 'value';
The table_name
should be user-specified, just as the value
is. So I would like to have a way to do something like:
my_queryset.filter(match=('table_name', 'value'))
However, this is not possible using lookups, as far as I’m aware of. That’s because lookups infer the field from the keyword arguments that get passed to methods like filter()
.
I have also thought about creating a custom implementation of Expression
, of Func
. That’s because my lookup could also be regarded as a function with an atypical syntax of param_1 MATCH 'param_2'
.
Nonetheless, I’m not sure if it would be a good way to approach this case (is it even possible to use a Func
object in a filter()
?).