Include related objects¶
Use include to request related resources in the response included section.
Basic include¶
import httpx
response = httpx.get("http://localhost:8000/api/customers/1?include=computers")
data = response.json()
print(data.get("included", []))
Example response shape:
{
"links": {
"self": "http://localhost:8000/api/customers/1?include=computers"
},
"data": {
"type": "customer",
"id": "1",
"attributes": {"name": "John"},
"links": {
"self": "http://localhost:8000/api/customers/1/"
},
"relationships": {
"computers": {
"data": [{"type": "computer", "id": "10"}],
"links": {
"self": "http://localhost:8000/api/customers/1/relationships/computers/",
"related": "http://localhost:8000/api/customers/1/computers/"
}
}
}
},
"included": [
{
"type": "computer",
"id": "10",
"attributes": {"serial": "ABC-123"},
"links": {
"self": "http://localhost:8000/api/computers/10/"
}
}
]
}
Nested include¶
Include with sparse fieldsets¶
import httpx
url = "http://localhost:8000/api/customers/1?include=computers&fields[customer]=name,computers&fields[computer]=serial"
response = httpx.get(url)
print(response.json())
Notes¶
includeworks on endpoints that return resource data.- Maximum depth is controlled by configuration (
MAX_INCLUDE_DEPTH).