Skip to content
Merged
Prev Previous commit
Minor refactoring based on feedback
  • Loading branch information
czosel committed May 10, 2017
commit fec7e0acb4b18b099f0d337b1aec22743e66a594
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Jerel Unruh <mail@unruhdesigns.com>
Greg Aker <greg@gregaker.net>
Adam Wróbel <https://adamwrobel.com>
Christian Zosel <https://zosel.ch>

1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ v2.3.0
attributes and relations to snake\_case format. This conversion was unexpected
and there was no way to turn it off.
* Fix for apps that don't use `django.contrib.contenttypes`.
* Fix `resource_name` support for POST requests and nested serializers

v2.2.0

Expand Down
8 changes: 5 additions & 3 deletions example/tests/integration/test_model_resource_name.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest
from copy import deepcopy
from example import models, serializers, views
from example.tests.utils import dump_json, load_json
from rest_framework import status
Expand Down Expand Up @@ -105,11 +106,12 @@ def test_model_resource_name_create(self, client):
def test_serializer_resource_name_create(self, client, monkeypatch):
monkeypatch.setattr(serializers.CommentSerializer.Meta, 'resource_name', 'renamed_comments', False)
monkeypatch.setattr(serializers.EntrySerializer.Meta, 'resource_name', 'renamed_entries', False)
self.create_data['data']['type'] = 'renamed_comments'
self.create_data['data']['relationships']['entry']['data']['type'] = 'renamed_entries'
create_data = deepcopy(self.create_data)
create_data['data']['type'] = 'renamed_comments'
create_data['data']['relationships']['entry']['data']['type'] = 'renamed_entries'

response = client.post(reverse("comment-list"),
dump_json(self.create_data),
dump_json(create_data),
content_type='application/vnd.api+json')

assert response.status_code == status.HTTP_201_CREATED
Expand Down
11 changes: 6 additions & 5 deletions rest_framework_json_api/relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from rest_framework.fields import MISSING_ERROR_MESSAGE, SerializerMethodField
from rest_framework.relations import *
from rest_framework.serializers import Serializer
from django.utils.translation import ugettext_lazy as _
from django.db.models.query import QuerySet

Expand Down Expand Up @@ -160,22 +161,22 @@ def get_resource_type_from_included_serializer(self):
included and return that name, or None
"""
field_name = self.field_name or self.parent.field_name
root = self.get_root_serializer()
parent = self.get_parent_serializer()

if root is not None:
if parent is not None:
# accept both singular and plural versions of field_name
field_names = [
inflection.singularize(field_name),
inflection.pluralize(field_name)
]
includes = get_included_serializers(root)
includes = get_included_serializers(parent)
for field in field_names:
if field in includes.keys():
return get_resource_type_from_serializer(includes[field])

return None

def get_root_serializer(self):
def get_parent_serializer(self):
if hasattr(self.parent, 'parent') and self.is_serializer(self.parent.parent):
return self.parent.parent
elif self.is_serializer(self.parent):
Expand All @@ -184,7 +185,7 @@ def get_root_serializer(self):
return None

def is_serializer(self, candidate):
return hasattr(candidate, 'included_serializers')
return isinstance(candidate, Serializer)

def get_choices(self, cutoff=None):
queryset = self.get_queryset()
Expand Down