Repository: django-json-api/django-rest-framework-json-api

Issue:
'ReturnList' object has no attribute 'get'
I am using the develop branch and get this in return when accessing a list:

'ReturnList' object has no attribute 'get'

```
                json_api_included.extend(included)
        else:
            json_api_data = data
    # Make sure we render data in a specific order
    render_data = OrderedDict()
                if data.get('links'): ...
        render_data['links'] = data.get('links')
    render_data['data'] = json_api_data
    if len(json_api_included) > 0:
        # Iterate through compound documents to remove duplicates
```

With the actual variable being

data  
[OrderedDict([(u'id', 2), (u'user', 2), (u'slug', u'asdfasdfsadf'),...])]

Using the latest DRF @ 18c93912180aa93d1120495d4f7e47d7bed025f4

I am using a ModelViewSet and the detail view works.



Discussion:
What are your DRF settings looking like ?

Also, if you use the latest release of DRF do you have the same issue? The version you are using is currently 285 commits ahead of the released version (3.1.3)

Dived a bit into some code and found that the object that throws an error is a ReturnList as defined by DRF at https://github.com/tomchristie/django-rest-framework/blob/3.1.3/rest_framework/utils/serializer_helpers.py

This is inheriting from Pythons list object. And I can't seem to find a get method defined there: https://docs.python.org/2/tutorial/datastructures.html

So either data.get is not the right function, or data should not be a ReturnList?

As to your questions: I did try with a few releases from DRF. 3.0.0, 3.1.0 and 3.1.3 all give the same error. Before 3.0.0 is a different error, but I guess no surprise there.

Settings:

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.TokenAuthentication',
        'rest_framework.authentication.BasicAuthentication',
        'rest_framework.authentication.SessionAuthentication',
    ),
    'DEFAULT_PARSER_CLASSES': (
        'rest_framework_json_api.parsers.JSONParser',
        'rest_framework.parsers.FormParser',
        # 'rest_framework.parsers.JSONParser',
        'rest_framework.parsers.MultiPartParser',
    ),
    'DEFAULT_RENDERER_CLASSES': (
        'rest_framework_json_api.renderers.JSONRenderer',
        'rest_framework.renderers.BrowsableAPIRenderer',
    ),
}

@philippeluickx unfortunately I just can't reproduce your problem. `data` is always a `dict` like structure for me and not a `ReturnList` in list views

list objects do not have the safe 'get' methods on them so this is why you get the error message you have.

I have a small test app I'm using, doesn't have much in it but it should isolate the problem real quick:
https://github.com/jsenecal/django-rest-framework-json-api_tests

There is no doc (sorry) but you should be able to find your way through it fast enough. Be sure to use a new virtualenv to avoid replicating issues that came from there.

List views without pagination with default DRF settings when passed to the renderer look like this (output from pycharm debugger):

``` python
data = {OrderedDict} OrderedDict([('count', 3), ('next', None), ('previous', None), ('results', [OrderedDict([('id', 2), ('last_login', None), ('is_superuser', False), ('username', 'test'), ('first_name', 'Test'), ('last_name', 'User'), ('email', 'user@gmail.com'), ('i...
 __len__ = {int} 4
 'count' (140557407234512) = {int} 3
 'next' (140557407383256) = {NoneType} None
 'previous' (140556856955504) = {NoneType} None
 'results' (140557404027288) = {ReturnList} [OrderedDict([('id', 2), ('last_login', None), ('is_superuser', False), ('username', 'test'), ('first_name', 'Test'), ('last_name', 'User'), ('email', 'user@gmail.com'), ('is_staff', False), ('is_active', True), ('date_joined', '2015-06-18T17:01:20...
```

I tested your repo, there everything is fine. I get dict objects returned:
<type 'dict'>

For my repo, I have <class 'rest_framework.utils.serializer_helpers.ReturnList'> returned. I do have some dependencies, checking these now. My view is pretty standard:

class ApplicationViewSet(ModelViewSet):
    """
    A simple ViewSet for viewing and editing applications.
    """
    serializer_class = ApplicationSerializer
    queryset = Application.objects.all()
    permission_classes = [
        IsAuthenticated,
        IsOwnerPermission,
    ]

Found the culprit: everything seems to work when adding PAGINATE_BY to my settings.

It does change the object being returned, as it now becomes a dictionary... Not greatly consistent behaviour upstream there, not sure if this should be handled in drf-json-api or straight in drf?

Ha! I think you found it, I'm not sure if this behavior is by-design though... We sure can check if data is in fact a `dict` before calling `.get()` on it!

I'll get something done by tomorrow, or if you feel like it, submit a PR ;)


Resolving patch:
diff --git a/rest_framework_json_api/renderers.py b/rest_framework_json_api/renderers.py
--- a/rest_framework_json_api/renderers.py
+++ b/rest_framework_json_api/renderers.py
@@ -87,7 +87,7 @@ def render(self, data, accepted_media_type=None, renderer_context=None):
         # Make sure we render data in a specific order
         render_data = OrderedDict()
 
-        if data.get('links'):
+        if isinstance(data, dict) and data.get('links'):
             render_data['links'] = data.get('links')
 
         render_data['data'] = json_api_data
@@ -105,7 +105,7 @@ def render(self, data, accepted_media_type=None, renderer_context=None):
             # Sort the items by type then by id
             render_data['included'] = sorted(unique_compound_documents, key=lambda item: (item['type'], item['id']))
 
-        if data.get('meta'):
+        if isinstance(data, dict) and data.get('meta'):
             render_data['meta'] = data.get('meta')
 
         return super(JSONRenderer, self).render(


Test patch: