File tree Expand file tree Collapse file tree 4 files changed +61
-12
lines changed Expand file tree Collapse file tree 4 files changed +61
-12
lines changed Original file line number Diff line number Diff 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
128128JSON_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
Original file line number Diff line number Diff line change 1111
1212JSON_API_FORMAT_KEYS = 'camelize'
1313JSON_API_FORMAT_RELATION_KEYS = 'camelize'
14+ JSON_API_PLURALIZE_RELATION_TYPE = True
1415REST_FRAMEWORK .update ({
1516 'PAGINATE_BY' : 1 ,
1617})
Original file line number Diff line number Diff line change 11import pytest
2-
32from django .conf import settings
43from django .contrib .auth import get_user_model
54from rest_framework import serializers
65from rest_framework .response import Response
76from rest_framework .views import APIView
7+
88from rest_framework_json_api import utils
99
1010pytestmark = pytest .mark .django_db
@@ -20,12 +20,12 @@ class Meta():
2020def 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 ()
Original file line number Diff line number Diff 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
152150def build_json_resource_obj (fields , resource , resource_instance , resource_name ):
You can’t perform that action at this time.
0 commit comments