mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
pk__notin= filter, closes #614
This commit is contained in:
parent
fa4d77b01e
commit
d3e9387466
3 changed files with 21 additions and 0 deletions
|
|
@ -77,6 +77,20 @@ class InFilter(Filter):
|
||||||
return "{} in {}".format(column, json.dumps(self.split_value(value)))
|
return "{} in {}".format(column, json.dumps(self.split_value(value)))
|
||||||
|
|
||||||
|
|
||||||
|
class NotInFilter(InFilter):
|
||||||
|
key = "notin"
|
||||||
|
display = "not in"
|
||||||
|
|
||||||
|
def where_clause(self, table, column, value, param_counter):
|
||||||
|
values = self.split_value(value)
|
||||||
|
params = [":p{}".format(param_counter + i) for i in range(len(values))]
|
||||||
|
sql = "{} not in ({})".format(escape_sqlite(column), ", ".join(params))
|
||||||
|
return sql, values
|
||||||
|
|
||||||
|
def human_clause(self, column, value):
|
||||||
|
return "{} not in {}".format(column, json.dumps(self.split_value(value)))
|
||||||
|
|
||||||
|
|
||||||
class Filters:
|
class Filters:
|
||||||
_filters = (
|
_filters = (
|
||||||
[
|
[
|
||||||
|
|
@ -125,6 +139,7 @@ class Filters:
|
||||||
TemplatedFilter("like", "like", '"{c}" like :{p}', '{c} like "{v}"'),
|
TemplatedFilter("like", "like", '"{c}" like :{p}', '{c} like "{v}"'),
|
||||||
TemplatedFilter("glob", "glob", '"{c}" glob :{p}', '{c} glob "{v}"'),
|
TemplatedFilter("glob", "glob", '"{c}" glob :{p}', '{c} glob "{v}"'),
|
||||||
InFilter(),
|
InFilter(),
|
||||||
|
NotInFilter(),
|
||||||
]
|
]
|
||||||
+ (
|
+ (
|
||||||
[
|
[
|
||||||
|
|
|
||||||
|
|
@ -228,6 +228,9 @@ You can filter the data returned by the table based on column values using a que
|
||||||
|
|
||||||
``?column__in=["value","value,with,commas"]``
|
``?column__in=["value","value,with,commas"]``
|
||||||
|
|
||||||
|
``?column__notin=value1,value2,value3``
|
||||||
|
Rows where column does not match any of the provided values. The inverse of ``__in=``. Also supports JSON arrays.
|
||||||
|
|
||||||
``?column__arraycontains=value``
|
``?column__arraycontains=value``
|
||||||
Works against columns that contain JSON arrays - matches if any of the values in that array match.
|
Works against columns that contain JSON arrays - matches if any of the values in that array match.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -47,6 +47,9 @@ import pytest
|
||||||
["foo in (:p0, :p1)"],
|
["foo in (:p0, :p1)"],
|
||||||
["dog,cat", "cat[dog]"],
|
["dog,cat", "cat[dog]"],
|
||||||
),
|
),
|
||||||
|
# Not in, and JSON array not in
|
||||||
|
((("foo__notin", "1,2,3"),), ["foo not in (:p0, :p1, :p2)"], ["1", "2", "3"]),
|
||||||
|
((("foo__notin", "[1,2,3]"),), ["foo not in (:p0, :p1, :p2)"], [1, 2, 3]),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
def test_build_where(args, expected_where, expected_params):
|
def test_build_where(args, expected_where, expected_params):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue