Skip to content

Commit 9f8c45c

Browse files
committed
added a get_resource_type_from_model
replaced other usages of getting model type by __name__
1 parent 42e0554 commit 9f8c45c

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

rest_framework_json_api/serializers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def to_representation(self, instance):
2929
}
3030

3131
def to_internal_value(self, data):
32-
if data['type'] != format_relation_name(self.model_class.__name__):
32+
if data['type'] != get_resource_type_from_model(self.model_class):
3333
self.fail('incorrect_model_type', model_type=self.model_class, received_type=data['type'])
3434
pk = data['id']
3535
try:

rest_framework_json_api/utils.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def get_resource_name(context):
5050
return get_resource_type_from_serializer(serializer)
5151
except AttributeError:
5252
try:
53-
resource_name = view.model.__name__
53+
resource_name = get_resource_type_from_model(view.model)
5454
except AttributeError:
5555
resource_name = view.__class__.__name__
5656

@@ -171,7 +171,7 @@ def get_related_resource_type(relation):
171171
relation_model = parent_model_relation.field.related.model
172172
else:
173173
return get_related_resource_type(parent_model_relation)
174-
return format_relation_name(relation_model.__name__)
174+
return get_resource_type_from_model(relation_model)
175175

176176

177177
def get_instance_or_manager_resource_type(resource_instance_or_manager):
@@ -182,25 +182,35 @@ def get_instance_or_manager_resource_type(resource_instance_or_manager):
182182
pass
183183

184184

185+
def get_resource_type_from_model(model):
186+
json_api_meta = getattr(model, 'JSONAPIMeta', None)
187+
if json_api_meta:
188+
resource_name = getattr(
189+
json_api_meta,
190+
'resource_name',
191+
format_relation_name(model.__name__))
192+
else:
193+
resource_name = format_relation_name(model.__name__)
194+
return resource_name
195+
196+
185197
def get_resource_type_from_queryset(qs):
186-
return format_relation_name(qs.model._meta.model.__name__)
198+
return get_resource_type_from_model(qs.model)
187199

188200

189201
def get_resource_type_from_instance(instance):
190-
return format_relation_name(instance._meta.model.__name__)
202+
return get_resource_type_from_model(instance._meta.model)
191203

192204

193205
def get_resource_type_from_manager(manager):
194-
return format_relation_name(manager.model.__name__)
206+
return get_resource_type_from_model(manager.model)
195207

196208

197209
def get_resource_type_from_serializer(serializer):
198-
try:
199-
# Check the meta class for resource_name
200-
return serializer.Meta.resource_name
201-
except AttributeError:
202-
# Use the serializer model then pluralize and format
203-
return format_relation_name(serializer.Meta.model.__name__)
210+
return getattr(
211+
serializer.Meta,
212+
'resource_name',
213+
get_resource_type_from_model(serializer.Meta.model))
204214

205215

206216
def get_included_serializers(serializer):

0 commit comments

Comments
 (0)