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
|
|
@ -1096,6 +1096,52 @@ def test_suggest_facets_off():
|
|||
).json["suggested_facets"]
|
||||
|
||||
|
||||
def test_expand_labels(app_client):
|
||||
response = app_client.get(
|
||||
"/test_tables/facetable.json?_shape=object&_labels=1&_size=2"
|
||||
"&neighborhood__contains=c"
|
||||
)
|
||||
assert {
|
||||
"2": {
|
||||
"pk": 2,
|
||||
"planet_int": 1,
|
||||
"state": "CA",
|
||||
"city_id": {
|
||||
"value": 1,
|
||||
"label": "San Francisco"
|
||||
},
|
||||
"neighborhood": "Dogpatch"
|
||||
},
|
||||
"13": {
|
||||
"pk": 13,
|
||||
"planet_int": 1,
|
||||
"state": "MI",
|
||||
"city_id": {
|
||||
"value": 3,
|
||||
"label": "Detroit"
|
||||
},
|
||||
"neighborhood": "Corktown"
|
||||
}
|
||||
} == response.json
|
||||
|
||||
|
||||
def test_expand_label(app_client):
|
||||
response = app_client.get(
|
||||
"/test_tables/foreign_key_references.json?_shape=object"
|
||||
"&_label=foreign_key_with_label"
|
||||
)
|
||||
assert {
|
||||
"1": {
|
||||
"pk": "1",
|
||||
"foreign_key_with_label": {
|
||||
"value": "1",
|
||||
"label": "hello"
|
||||
},
|
||||
"foreign_key_with_no_label": "1"
|
||||
}
|
||||
} == response.json
|
||||
|
||||
|
||||
@pytest.mark.parametrize('path,expected_cache_control', [
|
||||
("/test_tables/facetable.json", "max-age=31536000"),
|
||||
("/test_tables/facetable.json?_ttl=invalid", "max-age=31536000"),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue