inflection ignored in lookup by RelationshipView
See original GitHub issueI did a quick search and didn’t find a similar issue, but I apologize if I missed one on this topic. And, maybe this is an enhancement request rather than a bug report. It is an inconsistency in type names between the url and rendered document.
For an api view of a model I have eg http://localhost/api/customer-accounts/ where the relationship type (lets just call it relationship-name) is inflected correctly according to the setting JSON_API_FORMAT_TYPES (dasherized, in this case)
{
"links": {
"first": "http://localhost/api/customer-accounts/?page%5Bnumber%5D=1",
"last": "http://localhost/api/customer-accounts/?page%5Bnumber%5D=1",
"next": null,
"prev": null
},
"data": [
{
"type": "customer-accounts",
"id": "1",
"attributes": {
"account-id": 1,
"account": "1234",
"insert-ts": "2020-05-20T20:42:52.895506Z",
"update-ts": "2020-05-20T20:42:52.895643Z"
},
"relationships": {
"relationship-name": {
"data": {
"type": "relationship-name",
"id": "1"
}
}
}
}
],
"meta": {
"pagination": {
"page": 1,
"pages": 1,
"count": 1
}
}
}
Then I go to eg http://localhost/api/customer-accounts/1/relationships/relationship-name/ and get a 404. Instead, I find the relationship at http://localhost/api/customer-accounts/1/relationships/relationship_name/ which looks like:
{
"data": {
"type": "relationship-name",
"id": "1"
}
}
The rendered document has the correctly inflected type, which doesn’t match the url.
I am working around this for now using field name mapping in RelationshipView
field_name_mapping = {
'relationship-name': 'relationship_name'
}
Could RelationshipView pass relationship-name through the inflector first to get it back to the internal python (underscore) convention?
Issue Analytics
- State:
- Created 3 years ago
- Comments:8 (8 by maintainers)
Top GitHub Comments
When the feature of formatting is being used DJA assumes that the field names are named with the Python convention of underscoring. This is currently done in the parser so you can do it here too.
Thanks @platinumazure that you are willing to work on this issue.
I think inflection for relationship view should be the default as I consider this issue a bug. As this is not a minor change it is certainly good to implement it backwards compatible. I think easiest would be to implement a setting where inflection of relationship view can be turned on. However when the setting is off and someone uses the relationship view a deprecation warning can be raised.
In PR https://github.com/django-json-api/django-rest-framework-json-api/pull/776 a similar approach has been taken so you can find how to introduce a new setting and how to raise a deprecation warning.
Let me know if you need any more hinters.