kopia lustrzana https://github.com/simonw/datasette
pk__notin= filter, closes #614
rodzic
ee330222f4
commit
9db22cdf18
|
|
@ -77,6 +77,20 @@ class InFilter(Filter):
|
|||
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:
|
||||
_filters = (
|
||||
[
|
||||
|
|
@ -125,6 +139,7 @@ class Filters:
|
|||
TemplatedFilter("like", "like", '"{c}" like :{p}', '{c} like "{v}"'),
|
||||
TemplatedFilter("glob", "glob", '"{c}" glob :{p}', '{c} glob "{v}"'),
|
||||
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__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``
|
||||
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)"],
|
||||
["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):
|
||||
|
|
|
|||
Ładowanie…
Reference in New Issue