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
Toggle structure

Without the filter selected:

► Show result
Toggle structure

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

By default the names 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
Toggle structure

How do I filter on the thing itself?

Filtering a table on the thing itself is sometimes useful, but can be a bit unintuitive:

albums = Table(
    auto__model=Album,
    auto__include=['name', 'artist', 'year'],

    query__filters__album=Filter.choice_queryset(
        attr=None,
        choices=Album.objects.all(),
        value_to_q=lambda value_string_or_f, **_: Q(pk=value_string_or_f) if value_string_or_f else Q(),
    ),
)
▼ Hide result
Toggle structure

How do I style the query container?

The Query component renders a container element that wraps the filter form. You can configure its tag and attributes directly:

albums = Table(
    auto__model=Album,
    auto__include=['name', 'artist', 'year'],
    columns__name__filter__include=True,
    query__attrs__style__border='2px solid red',
)
▼ Hide result
Toggle structure