Field lookups... but not on fields

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()?).

I’m not quite sure I’m following what you’re trying to achieve here.

From what I can find in the SQLite docs:

The MATCH operator is a special syntax for the match() application-defined function. The default match() function implementation raises an exception and is not really useful for anything. But extensions can override the match() function with more helpful logic.

So then it looks to me like you’re trying to use a UDF within the where clause of your query. Am I correct?

But yes, it appears to me that the easiest way of implementing this would be a custom expression.