Queries

How do I override what operator is used for a query?

The member query_operator_to_q_operator for Filter is used to convert from e.g. : to icontains. You can specify another callable here:

Table(
    auto__model=Track,
    columns__album__filter__query_operator_to_q_operator=lambda op: 'exact',
)

The above will force the album name to always be looked up with case sensitive match even if the user types album<Paranoid in the advanced query language. Use this feature with caution!

See also How do I control what Q is produced?

How do I control what Q is produced?

For more advanced customization you can use value_to_q. It is a callable that takes filter, op, value_string_or_f and returns a Q object. The default handles __, different operators, negation and special handling of when the user searches for null.

class AlbumTable(Table):
    class Meta:
        auto__model = Album

        query__form__fields__eighties = Field.boolean(
            display_name="the '80s",
        )

        @staticmethod
        def query__filters__eighties__value_to_q(value_string_or_f, **_):
            if value_string_or_f == "1":
                return Q(year__gte=1980) & Q(year__lte=1989)
            return Q()
▼ Hide result

Without the filter selected:

► Show result

How do I control the name used in the advanced query?

By default the name of filters are derived from the name you specify or from the model field name For deeply nested names double underscores are replaced with single underscores, and those names can become a bit unwieldy. You can then override this with query_name:

track_table = Table(
    auto__model=Track,
    auto__include=['name', 'album', 'album__artist__name'],
    columns__album_artist_name__filter=dict(
        include=True,
        query_name='artist',
    ),
)
▼ Hide result