@@ -67,10 +67,10 @@ def get_resource_name(context):
6767 if not isinstance (resource_name , six .string_types ):
6868 return resource_name
6969
70- resource_name = inflection .pluralize (resource_name .lower ())
71-
7270 resource_name = format_value (resource_name )
7371
72+ resource_name = inflection .pluralize (resource_name )
73+
7474 return resource_name
7575
7676
@@ -91,17 +91,22 @@ def format_keys(obj, format_type=None):
9191 if format_type is None :
9292 format_type = getattr (settings , 'JSON_API_FORMAT_KEYS' , False )
9393
94- if format_type in ('dasherize' , 'camelize' , 'underscore' ):
94+ if format_type in ('dasherize' , 'camelize' , 'underscore' , 'capitalize' ):
9595
9696 if isinstance (obj , dict ):
9797 formatted = OrderedDict ()
9898 for key , value in obj .items ():
9999 if format_type == 'dasherize' :
100+ # inflection can't dasherize camelCase
101+ key = inflection .underscore (key )
100102 formatted [inflection .dasherize (key )] \
101103 = format_keys (value , format_type )
102104 elif format_type == 'camelize' :
103105 formatted [inflection .camelize (key , False )] \
104106 = format_keys (value , format_type )
107+ elif format_type == 'capitalize' :
108+ formatted [inflection .camelize (key )] \
109+ = format_keys (value , format_type )
105110 elif format_type == 'underscore' :
106111 formatted [inflection .underscore (key )] \
107112 = format_keys (value , format_type )
@@ -118,8 +123,12 @@ def format_value(value, format_type=None):
118123 if format_type is None :
119124 format_type = getattr (settings , 'JSON_API_FORMAT_KEYS' , False )
120125 if format_type == 'dasherize' :
126+ # inflection can't dasherize camelCase
127+ value = inflection .underscore (value )
121128 value = inflection .dasherize (value )
122129 elif format_type == 'camelize' :
130+ value = inflection .camelize (value , False )
131+ elif format_type == 'capitalize' :
123132 value = inflection .camelize (value )
124133 elif format_type == 'underscore' :
125134 value = inflection .underscore (value )
@@ -130,6 +139,9 @@ def format_relation_name(value, format_type=None):
130139 if format_type is None :
131140 format_type = getattr (settings , 'JSON_API_FORMAT_RELATION_KEYS' , False )
132141
142+ if not format_type :
143+ return value
144+
133145 # format_type will never be None here so we can use format_value
134146 value = format_value (value , format_type )
135147
@@ -153,7 +165,9 @@ def build_json_resource_obj(fields, resource, resource_instance, resource_name):
153165
154166
155167def get_related_resource_type (relation ):
156- if hasattr (relation , 'get_queryset' ) and relation .get_queryset () is not None :
168+ if hasattr (relation , '_meta' ):
169+ relation_model = relation ._meta .model
170+ elif hasattr (relation , 'get_queryset' ) and relation .get_queryset () is not None :
157171 relation_model = relation .get_queryset ().model
158172 else :
159173 parent_serializer = relation .parent
@@ -265,11 +279,10 @@ def extract_relationships(fields, resource, resource_instance):
265279
266280 if isinstance (field , ManyRelatedField ):
267281 relation_data = list ()
268- related_object = field .child_relation
269- relation_type = get_related_resource_type (related_object )
270282 for related_object in relation_instance_or_manager .all ():
283+ related_object_type = get_related_resource_type (related_object )
271284 relation_data .append (OrderedDict ([
272- ('type' , relation_type ),
285+ ('type' , related_object_type ),
273286 ('id' , encoding .force_text (related_object .pk ))
274287 ]))
275288 data .update ({
@@ -284,20 +297,18 @@ def extract_relationships(fields, resource, resource_instance):
284297
285298 if isinstance (field , ListSerializer ):
286299 relation_data = list ()
287- serializer = field .child
288- relation_model = serializer .Meta .model
289- relation_type = format_relation_name (relation_model .__name__ )
290300
291301 serializer_data = resource .get (field_name )
292302 resource_instance_queryset = relation_instance_or_manager .all ()
293303 if isinstance (serializer_data , list ):
294304 for position in range (len (serializer_data )):
295305 nested_resource_instance = resource_instance_queryset [position ]
296- relation_data .append (
297- OrderedDict (
298- [('type' , relation_type ), ('id' , encoding .force_text (nested_resource_instance .pk ))]
299- )
300- )
306+ nested_resource_instance_type = get_related_resource_type (
307+ nested_resource_instance )
308+ relation_data .append (OrderedDict ([
309+ ('type' , nested_resource_instance_type ),
310+ ('id' , encoding .force_text (nested_resource_instance .pk ))
311+ ]))
301312
302313 data .update ({field_name : {'data' : relation_data }})
303314 continue
0 commit comments