Handle multiple m2m args of same name

e.g. ?_m2m_ad_targets__target_id=ec3ac&_m2m_ad_targets__target_id=e128e
This commit is contained in:
Simon Willison 2018-07-30 20:54:38 -07:00
commit 04c7fd6207
No known key found for this signature in database
GPG key ID: 17E2DEA2588B7F52

View file

@ -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")