Skip to content

Commit 0382699

Browse files
committed
Merge pull request #76 from adfinis-sygroup/develop
Added a feature to format the relation names
2 parents d997b0b + 9b00d1b commit 0382699

File tree

2 files changed

+75
-6
lines changed

2 files changed

+75
-6
lines changed

docs/usage.md

+55-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ If you set the `resource_name` property on the object to `False` the data
5555
will be returned without modification.
5656

5757

58-
### Inflecting object keys
58+
### Inflecting object and relation keys
5959

6060
This package includes the ability (off by default) to automatically convert json
6161
requests and responses from the python/rest_framework's preferred underscore to
@@ -120,6 +120,60 @@ Example - With format conversion set to `dasherize`:
120120
}
121121
```
122122

123+
#### Relationship types
124+
125+
A similar option to JSON\_API\_FORMAT\_KEYS can be set for the relationship names:
126+
127+
``` python
128+
JSON_API_FORMAT_RELATION_KEYS = 'dasherize'
129+
```
130+
131+
Example without format conversion:
132+
133+
``` js
134+
{
135+
"data": [{
136+
"type": "identities",
137+
"id": 3,
138+
"attributes": {
139+
...
140+
},
141+
"relationships": {
142+
"home_town": {
143+
"data": [{
144+
"type": "home_town",
145+
"id": 3
146+
}]
147+
}
148+
}
149+
}]
150+
}
151+
```
152+
153+
When set to dasherize:
154+
155+
156+
``` js
157+
{
158+
"data": [{
159+
"type": "identities",
160+
"id": 3,
161+
"attributes": {
162+
...
163+
},
164+
"relationships": {
165+
"home_town": {
166+
"data": [{
167+
"type": "home-town",
168+
"id": 3
169+
}]
170+
}
171+
}
172+
}]
173+
}
174+
```
175+
176+
123177
<!--
124178
### Relationships
125179
### Links

rest_framework_json_api/utils.py

+20-5
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,21 @@ def format_value(value, format_type=None):
128128
return value
129129

130130

131+
def format_relation_name(value, format_type=None):
132+
format_type = getattr(settings, 'JSON_API_FORMAT_RELATION_KEYS', False)
133+
134+
value = inflection.underscore(value)
135+
136+
if format_type == 'dasherize':
137+
value = inflection.dasherize(value)
138+
elif format_type == 'camelize':
139+
value = inflection.camelize(value)
140+
elif format_type == 'underscore':
141+
value = inflection.underscore(value)
142+
143+
return inflection.pluralize(value)
144+
145+
131146
def build_json_resource_obj(fields, resource, resource_instance, resource_name):
132147
resource_data = [
133148
('type', resource_name),
@@ -168,7 +183,7 @@ def get_related_resource_type(relation):
168183
relation_model = parent_model_relation.field.related.model
169184
else:
170185
raise APIException('Unable to find related model for relation {relation}'.format(relation=relation))
171-
return inflection.pluralize(relation_model.__name__).lower()
186+
return format_relation_name(relation_model.__name__)
172187

173188

174189
def extract_id_from_url(url):
@@ -280,7 +295,7 @@ def extract_relationships(fields, resource, resource_instance):
280295

281296
serializer = field.child
282297
relation_model = serializer.Meta.model
283-
relation_type = inflection.pluralize(relation_model.__name__).lower()
298+
relation_type = format_relation_name(relation_model.__name__)
284299

285300
# Get the serializer fields
286301
serializer_fields = get_serializer_fields(serializer)
@@ -297,7 +312,7 @@ def extract_relationships(fields, resource, resource_instance):
297312

298313
if isinstance(field, ModelSerializer):
299314
relation_model = field.Meta.model
300-
relation_type = inflection.pluralize(relation_model.__name__).lower()
315+
relation_type = format_relation_name(relation_model.__name__)
301316

302317
# Get the serializer fields
303318
serializer_fields = get_serializer_fields(field)
@@ -331,7 +346,7 @@ def extract_included(fields, resource, resource_instance):
331346

332347
serializer = field.child
333348
model = serializer.Meta.model
334-
relation_type = inflection.pluralize(model.__name__).lower()
349+
relation_type = format_relation_name(model.__name__)
335350

336351
# Get the serializer fields
337352
serializer_fields = get_serializer_fields(serializer)
@@ -350,7 +365,7 @@ def extract_included(fields, resource, resource_instance):
350365
if isinstance(field, ModelSerializer):
351366

352367
model = field.Meta.model
353-
relation_type = inflection.pluralize(model.__name__).lower()
368+
relation_type = format_relation_name(model.__name__)
354369

355370
# Get the serializer fields
356371
serializer_fields = get_serializer_fields(field)

0 commit comments

Comments
 (0)