diff --git a/datasette/filters.py b/datasette/filters.py index 4d9580d8..585d4865 100644 --- a/datasette/filters.py +++ b/datasette/filters.py @@ -281,6 +281,13 @@ class Filters: '{c} contains "{v}"', format="%{}%", ), + TemplatedFilter( + "notcontains", + "does not contain", + '"{c}" not like :{p}', + '{c} does not contain "{v}"', + format="%{}%", + ), TemplatedFilter( "endswith", "ends with", diff --git a/docs/json_api.rst b/docs/json_api.rst index 366f74b2..4b39a048 100644 --- a/docs/json_api.rst +++ b/docs/json_api.rst @@ -237,6 +237,9 @@ You can filter the data returned by the table based on column values using a que ``?column__contains=value`` Rows where the string column contains the specified value (``column like "%value%"`` in SQL). +``?column__notcontains=value`` + Rows where the string column does not contain the specified value (``column not like "%value%"`` in SQL). + ``?column__endswith=value`` Rows where the string column ends with the specified value (``column like "%value"`` in SQL). diff --git a/tests/test_filters.py b/tests/test_filters.py index 5b2e9636..a3fada98 100644 --- a/tests/test_filters.py +++ b/tests/test_filters.py @@ -7,6 +7,11 @@ import pytest "args,expected_where,expected_params", [ ((("name_english__contains", "foo"),), ['"name_english" like :p0'], ["%foo%"]), + ( + (("name_english__notcontains", "foo"),), + ['"name_english" not like :p0'], + ["%foo%"], + ), ( (("foo", "bar"), ("bar__contains", "baz")), ['"bar" like :p0', '"foo" = :p1'],