Inflection (Attribute Key Transformation)¶
By default, attribute keys in JSON:API responses match the Python field names on your Pydantic schema (e.g. first_name). You can configure django-ninja-jsonapi to automatically transform keys to dasherized or camelCase form.
Configuration¶
Add INFLECTION to your NINJA_JSONAPI settings:
| Value | Python field | JSON:API key |
|---|---|---|
None |
first_name |
first_name |
"dasherize" |
first_name |
first-name |
"camelize" |
first_name |
firstName |
How it works¶
When inflection is enabled:
- Outgoing responses — attribute keys are transformed before serialisation, so
first_nameappears asfirst-nameorfirstNamein the JSON document. - Sparse fieldsets —
fields[customer]=first-nameis accepted and mapped back to the underlyingfirst_namefield. - Schema aliases — dynamically built Pydantic models get an
alias_generatorsomodel_dump(by_alias=True)produces transformed keys automatically.
Example¶
Schema:
With "INFLECTION": "dasherize":
{
"data": {
"type": "customer",
"id": "1",
"attributes": {
"first-name": "Alice",
"last-name": "Smith"
}
}
}
Manual use¶
The inflection functions are available for direct use in custom code: