Skip to content

Commit 528eb04

Browse files
committed
Merge pull request #106 from django-json-api/feature/pluralize
Feature/pluralize
2 parents 63ff17b + ca48dcb commit 528eb04

File tree

4 files changed

+61
-12
lines changed

4 files changed

+61
-12
lines changed

docs/usage.md

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ Example - With format conversion set to `dasherize`:
122122

123123
#### Relationship types
124124

125-
A similar option to JSON\_API\_FORMAT\_KEYS can be set for the relationship names:
125+
A similar option to JSON\_API\_FORMAT\_RELATION\_KEYS can be set for the relationship names:
126126

127127
``` python
128128
JSON_API_FORMAT_RELATION_KEYS = 'dasherize'
@@ -172,7 +172,57 @@ When set to dasherize:
172172
}]
173173
}
174174
```
175+
It is also possible to pluralize the types like so:
175176

177+
```python
178+
JSON_API_PLURALIZE_RELATION_TYPE = True
179+
```
180+
Example without pluralization:
181+
182+
``` js
183+
{
184+
"data": [{
185+
"type": "identity",
186+
"id": 3,
187+
"attributes": {
188+
...
189+
},
190+
"relationships": {
191+
"home_towns": {
192+
"data": [{
193+
"type": "home_town",
194+
"id": 3
195+
}]
196+
}
197+
}
198+
}]
199+
}
200+
```
201+
202+
When set to pluralize:
203+
204+
205+
``` js
206+
{
207+
"data": [{
208+
"type": "identities",
209+
"id": 3,
210+
"attributes": {
211+
...
212+
},
213+
"relationships": {
214+
"home_towns": {
215+
"data": [{
216+
"type": "home_towns",
217+
"id": 3
218+
}]
219+
}
220+
}
221+
}]
222+
}
223+
224+
Both `JSON_API_PLURALIZE_RELATION_TYPE` and `JSON_API_FORMAT_RELATION_KEYS` can be combined to
225+
achieve different results.
176226

177227
<!--
178228
### Relationships

example/settings/test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
JSON_API_FORMAT_KEYS = 'camelize'
1313
JSON_API_FORMAT_RELATION_KEYS = 'camelize'
14+
JSON_API_PLURALIZE_RELATION_TYPE = True
1415
REST_FRAMEWORK.update({
1516
'PAGINATE_BY': 1,
1617
})

example/tests/test_utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import pytest
2-
32
from django.conf import settings
43
from django.contrib.auth import get_user_model
54
from rest_framework import serializers
65
from rest_framework.response import Response
76
from rest_framework.views import APIView
7+
88
from rest_framework_json_api import utils
99

1010
pytestmark = pytest.mark.django_db
@@ -20,12 +20,12 @@ class Meta():
2020
def test_get_resource_name():
2121
view = ResourceView()
2222
context = {'view': view}
23-
setattr(settings, 'JSON_API_FORMAT_KEYS', None)
23+
setattr(settings, 'JSON_API_FORMAT_RELATION_KEYS', None)
2424
assert 'ResourceViews' == utils.get_resource_name(context), 'not formatted'
2525

2626
view = ResourceView()
2727
context = {'view': view}
28-
setattr(settings, 'JSON_API_FORMAT_KEYS', 'dasherize')
28+
setattr(settings, 'JSON_API_FORMAT_RELATION_KEYS', 'dasherize')
2929
assert 'resource-views' == utils.get_resource_name(context), 'derived from view'
3030

3131
view.model = get_user_model()

rest_framework_json_api/utils.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,7 @@ def get_resource_name(context):
6868
if not isinstance(resource_name, six.string_types):
6969
return resource_name
7070

71-
resource_name = format_value(resource_name)
72-
73-
resource_name = inflection.pluralize(resource_name)
71+
resource_name = format_relation_name(resource_name)
7472

7573
return resource_name
7674

@@ -140,13 +138,13 @@ def format_relation_name(value, format_type=None):
140138
if format_type is None:
141139
format_type = getattr(settings, 'JSON_API_FORMAT_RELATION_KEYS', False)
142140

143-
if not format_type:
144-
return value
141+
pluralize = getattr(settings, 'JSON_API_PLURALIZE_RELATION_TYPE', False)
145142

146-
# format_type will never be None here so we can use format_value
147-
value = format_value(value, format_type)
143+
if format_type:
144+
# format_type will never be None here so we can use format_value
145+
value = format_value(value, format_type)
148146

149-
return inflection.pluralize(value)
147+
return inflection.pluralize(value) if pluralize else value
150148

151149

152150
def build_json_resource_obj(fields, resource, resource_instance, resource_name):

0 commit comments

Comments
 (0)