mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
?_labels= and ?_label=COL to expand foreign keys in JSON/CSV
These new querystring arguments can be used to request expanded foreign keys
in both JSON and CSV formats.
?_labels=on turns on expansions for ALL foreign key columns
?_label=COLUMN1&_label=COLUMN2 can be used to pick specific columns to expand
e.g. `Street_Tree_List.json?_label=qSpecies&_label=qLegalStatus`
{
"rowid": 233,
"TreeID": 121240,
"qLegalStatus": {
"value" 2,
"label": "Private"
}
"qSpecies": {
"value": 16,
"label": "Sycamore"
}
"qAddress": "91 Commonwealth Ave",
...
}
The labels option also works for the HTML and CSV views.
HTML defaults to `?_labels=on`, so if you pass `?_labels=off` you can disable
foreign key expansion entirely - or you can use `?_label=COLUMN` to request
just specific columns.
If you expand labels on CSV you get additional columns in the output:
`/Street_Tree_List.csv?_label=qLegalStatus`
rowid,TreeID,qLegalStatus,qLegalStatus_label...
1,141565,1,Permitted Site...
2,232565,2,Undocumented...
I also refactored the existing foreign key expansion code.
Closes #233. Refs #266.
This commit is contained in:
parent
0c22fa8f09
commit
ed631e690b
9 changed files with 276 additions and 69 deletions
|
|
@ -11,6 +11,24 @@ hello
|
|||
world
|
||||
'''.replace('\n', '\r\n')
|
||||
|
||||
EXPECTED_TABLE_WITH_LABELS_CSV = '''
|
||||
pk,planet_int,state,city_id,city_id_label,neighborhood
|
||||
1,1,CA,1,San Francisco,Mission
|
||||
2,1,CA,1,San Francisco,Dogpatch
|
||||
3,1,CA,1,San Francisco,SOMA
|
||||
4,1,CA,1,San Francisco,Tenderloin
|
||||
5,1,CA,1,San Francisco,Bernal Heights
|
||||
6,1,CA,1,San Francisco,Hayes Valley
|
||||
7,1,CA,2,Los Angeles,Hollywood
|
||||
8,1,CA,2,Los Angeles,Downtown
|
||||
9,1,CA,2,Los Angeles,Los Feliz
|
||||
10,1,CA,2,Los Angeles,Koreatown
|
||||
11,1,MI,3,Detroit,Downtown
|
||||
12,1,MI,3,Detroit,Greektown
|
||||
13,1,MI,3,Detroit,Corktown
|
||||
14,1,MI,3,Detroit,Mexicantown
|
||||
15,2,MC,4,Memnonia,Arcadia Planitia
|
||||
'''.lstrip().replace('\n', '\r\n')
|
||||
|
||||
def test_table_csv(app_client):
|
||||
response = app_client.get('/test_tables/simple_primary_key.csv')
|
||||
|
|
@ -19,6 +37,13 @@ def test_table_csv(app_client):
|
|||
assert EXPECTED_TABLE_CSV == response.text
|
||||
|
||||
|
||||
def test_table_csv_with_labels(app_client):
|
||||
response = app_client.get('/test_tables/facetable.csv?_labels=1')
|
||||
assert response.status == 200
|
||||
assert 'text/plain; charset=utf-8' == response.headers['Content-Type']
|
||||
assert EXPECTED_TABLE_WITH_LABELS_CSV == response.text
|
||||
|
||||
|
||||
def test_custom_sql_csv(app_client):
|
||||
response = app_client.get(
|
||||
'/test_tables.csv?sql=select+content+from+simple_primary_key+limit+2'
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue