From f99c2f5f8cd1550442f69def0b27f8ec799d6bd8 Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Tue, 27 Feb 2024 16:07:37 -0800 Subject: [PATCH] ?column_notcontains= table filter, closes #2287 --- datasette/filters.py | 7 +++++++ docs/json_api.rst | 3 +++ tests/test_filters.py | 5 +++++ 3 files changed, 15 insertions(+) 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'],