@@ -96,15 +96,31 @@ def get_serializer_fields(serializer):
96
96
return fields
97
97
98
98
99
- def format_keys (obj , format_type = None ):
99
+ def format_keys (obj , format_type = None , recursive = None ):
100
100
"""
101
- Takes either a dict or list and returns it with camelized keys only if
102
- JSON_API_FORMAT_KEYS is set.
101
+ Takes either a dict or list and formats keys as configured in
102
+ `JSON_API_FORMAT_KEYS` if set. `JSON_API_FIELD_NAMES` takes precedence
103
+ over `JSON_API_FORMAT_KEYS` but only formats field names preserving
104
+ values.
103
105
104
- :format_type: Either 'dasherize', 'camelize' or 'underscore'
106
+ :format_type: Either 'dasherize', 'camelize', 'capitalize' or 'underscore'
107
+ :recursive: If set all keys will be formatted; otherwise only keys of given object
105
108
"""
106
109
if format_type is None :
107
- format_type = getattr (settings , 'JSON_API_FORMAT_KEYS' , False )
110
+ format_type_field_names = getattr (settings , 'JSON_API_FORMAT_FIELD_NAMES' , False )
111
+ format_type_format_keys = getattr (settings , 'JSON_API_FORMAT_KEYS' , False )
112
+
113
+ if format_type_field_names and format_type_format_keys :
114
+ raise RuntimeError (
115
+ 'Setting `JSON_API_FORMAT_FIELD_NAMES` and `JSON_API_FORMAT_KEYS` '
116
+ 'may not both be set.'
117
+ )
118
+
119
+ format_type = format_type_field_names or format_type_format_keys
120
+
121
+ # only format keys of values when `FORMAT_KEYS` is set
122
+ if recursive is None :
123
+ recursive = bool (getattr (settings , 'JSON_API_FORMAT_KEYS' , False ))
108
124
109
125
if format_type in ('dasherize' , 'camelize' , 'underscore' , 'capitalize' ):
110
126
@@ -115,18 +131,18 @@ def format_keys(obj, format_type=None):
115
131
# inflection can't dasherize camelCase
116
132
key = inflection .underscore (key )
117
133
formatted [inflection .dasherize (key )] \
118
- = format_keys (value , format_type )
134
+ = recursive and format_keys (value , format_type ) or value
119
135
elif format_type == 'camelize' :
120
136
formatted [inflection .camelize (key , False )] \
121
- = format_keys (value , format_type )
137
+ = recursive and format_keys (value , format_type ) or value
122
138
elif format_type == 'capitalize' :
123
139
formatted [inflection .camelize (key )] \
124
- = format_keys (value , format_type )
140
+ = recursive and format_keys (value , format_type ) or value
125
141
elif format_type == 'underscore' :
126
142
formatted [inflection .underscore (key )] \
127
- = format_keys (value , format_type )
143
+ = recursive and format_keys (value , format_type ) or value
128
144
return formatted
129
- if isinstance (obj , list ):
145
+ if isinstance (obj , list ) and recursive :
130
146
return [format_keys (item , format_type ) for item in obj ]
131
147
else :
132
148
return obj
@@ -136,7 +152,11 @@ def format_keys(obj, format_type=None):
136
152
137
153
def format_value (value , format_type = None ):
138
154
if format_type is None :
139
- format_type = getattr (settings , 'JSON_API_FORMAT_KEYS' , False )
155
+ format_type = getattr (
156
+ settings ,
157
+ 'JSON_API_FORMAT_FIELD_NAMES' ,
158
+ getattr (settings , 'JSON_API_FORMAT_KEYS' , False )
159
+ )
140
160
if format_type == 'dasherize' :
141
161
# inflection can't dasherize camelCase
142
162
value = inflection .underscore (value )
0 commit comments