Skip to content

Feature to format the relation names #76

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 15, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 55 additions & 1 deletion docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ If you set the `resource_name` property on the object to `False` the data
will be returned without modification.


### Inflecting object keys
### Inflecting object and relation keys

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

#### Relationship types

A similar option to JSON\_API\_FORMAT\_KEYS can be set for the relationship names:

``` python
JSON_API_FORMAT_RELATION_KEYS = 'dasherize'
```

Example without format conversion:

``` js
{
"data": [{
"type": "identities",
"id": 3,
"attributes": {
...
},
"relationships": {
"home_town": {
"data": [{
"type": "home_town",
"id": 3
}]
}
}
}]
}
```

When set to dasherize:


``` js
{
"data": [{
"type": "identities",
"id": 3,
"attributes": {
...
},
"relationships": {
"home_town": {
"data": [{
"type": "home-town",
"id": 3
}]
}
}
}]
}
```


<!--
### Relationships
### Links
Expand Down
25 changes: 20 additions & 5 deletions rest_framework_json_api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,21 @@ def format_value(value, format_type=None):
return value


def format_relation_name(value, format_type=None):
format_type = getattr(settings, 'JSON_API_FORMAT_RELATION_KEYS', False)

value = inflection.underscore(value)

if format_type == 'dasherize':
value = inflection.dasherize(value)
elif format_type == 'camelize':
value = inflection.camelize(value)
elif format_type == 'underscore':
value = inflection.underscore(value)

return inflection.pluralize(value)


def build_json_resource_obj(fields, resource, resource_instance, resource_name):
resource_data = [
('type', resource_name),
Expand Down Expand Up @@ -168,7 +183,7 @@ def get_related_resource_type(relation):
relation_model = parent_model_relation.field.related.model
else:
raise APIException('Unable to find related model for relation {relation}'.format(relation=relation))
return inflection.pluralize(relation_model.__name__).lower()
return format_relation_name(relation_model.__name__)


def extract_id_from_url(url):
Expand Down Expand Up @@ -280,7 +295,7 @@ def extract_relationships(fields, resource, resource_instance):

serializer = field.child
relation_model = serializer.Meta.model
relation_type = inflection.pluralize(relation_model.__name__).lower()
relation_type = format_relation_name(relation_model.__name__)

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

if isinstance(field, ModelSerializer):
relation_model = field.Meta.model
relation_type = inflection.pluralize(relation_model.__name__).lower()
relation_type = format_relation_name(relation_model.__name__)

# Get the serializer fields
serializer_fields = get_serializer_fields(field)
Expand Down Expand Up @@ -331,7 +346,7 @@ def extract_included(fields, resource, resource_instance):

serializer = field.child
model = serializer.Meta.model
relation_type = inflection.pluralize(model.__name__).lower()
relation_type = format_relation_name(model.__name__)

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

model = field.Meta.model
relation_type = inflection.pluralize(model.__name__).lower()
relation_type = format_relation_name(model.__name__)

# Get the serializer fields
serializer_fields = get_serializer_fields(field)
Expand Down