From eeab4607a15960e0d784c1129dbfa7830b35b1c7 Mon Sep 17 00:00:00 2001 From: Stefan Heinemann Date: Fri, 11 Sep 2015 14:48:51 +0200 Subject: [PATCH 1/3] Feature to format the relation names Implemented a new option to be able to format the key names in relations. --- rest_framework_json_api/utils.py | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/rest_framework_json_api/utils.py b/rest_framework_json_api/utils.py index f51358f2..f0fc4d45 100644 --- a/rest_framework_json_api/utils.py +++ b/rest_framework_json_api/utils.py @@ -128,6 +128,20 @@ 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 value + + def build_json_resource_obj(fields, resource, resource_instance, resource_name): resource_data = [ ('type', resource_name), @@ -168,7 +182,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 inflection.pluralize(format_relation_name(relation_model.__name__)) def extract_id_from_url(url): @@ -280,7 +294,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 = inflection.pluralize(format_relation_name(relation_model.__name__)) # Get the serializer fields serializer_fields = get_serializer_fields(serializer) @@ -297,7 +311,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 = inflection.pluralize(format_relation_name(relation_model.__name__)) # Get the serializer fields serializer_fields = get_serializer_fields(field) @@ -331,7 +345,7 @@ def extract_included(fields, resource, resource_instance): serializer = field.child model = serializer.Meta.model - relation_type = inflection.pluralize(model.__name__).lower() + relation_type = inflection.pluralize(format_relation_name(model.__name__)) # Get the serializer fields serializer_fields = get_serializer_fields(serializer) @@ -350,7 +364,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 = inflection.pluralize(format_relation_name(model.__name__)) # Get the serializer fields serializer_fields = get_serializer_fields(field) From 97cec7525f21b72ee6eb7c652397d3c826592528 Mon Sep 17 00:00:00 2001 From: Stefan Heinemann Date: Tue, 15 Sep 2015 09:52:48 +0200 Subject: [PATCH 2/3] Documented the new relation format option --- docs/usage.md | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/docs/usage.md b/docs/usage.md index 366b17a2..146c24ff 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -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 @@ -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 + }] + } + } + }] +} +``` + +