From 04c7fd62077e7daffdaa92f00a2e4e51cbbad3a1 Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Mon, 30 Jul 2018 20:54:38 -0700 Subject: [PATCH] Handle multiple m2m args of same name e.g. ?_m2m_ad_targets__target_id=ec3ac&_m2m_ad_targets__target_id=e128e --- datasette/views/table.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/datasette/views/table.py b/datasette/views/table.py index 9e9be31a..6562bdaa 100644 --- a/datasette/views/table.py +++ b/datasette/views/table.py @@ -305,7 +305,7 @@ class TableView(RowTableShared): where_clauses, params = filters.build_where_clauses() # Hacky thing for ?_m2m_ad_targets__target_id=9a8c6 - for m2m_key in special_args: + for m2m_key in request.args: if m2m_key.startswith("_m2m_"): rest = m2m_key.split("_m2m_", 1)[1] m2m_table, other_column = rest.split("__", 1) @@ -317,16 +317,17 @@ class TableView(RowTableShared): # if fk["other_table"] != table # and fk["other_column"] == other_column # ][0] - value = special_args[m2m_key] - # Figure out what the columns are in that m2m table - where_clauses.append( - '{our_pk} in (select {our_pk} from {m2m_table} where {other_column} = "{value}")'.format( - m2m_table=escape_sqlite(m2m_table), - our_pk=fk_to_us["other_column"], - other_column=escape_sqlite(other_column), - value=value, + for value in request.args[m2m_key]: + # Figure out what the columns are in that m2m table + where_clauses.append( + '{our_pk} in (select {our_column} from {m2m_table} where {other_column} = "{value}")'.format( + m2m_table=escape_sqlite(m2m_table), + our_pk=fk_to_us["other_column"], + our_column=fk_to_us["column"], + other_column=escape_sqlite(other_column), + value=value, + ) ) - ) # _search support: fts_table = info[name]["tables"].get(table, {}).get("fts_table")