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

Issue:
'resource_instance' not needed when building resource object
I have

```
class LanguageSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Language
        fields = ('id', 'url', 'name')

class LanguageViewSet(viewsets.ModelViewSet):
    queryset = Language.objects.all()
    serializer_class = serializers.LanguageSerializer
```

Using DRF browsable api with rest_framework_json_api JSONRenderer throws exception when navigate to localhost:8000/api/languages/:

```
File "python_venv/lib/python3.4/site-packages/django/core/handlers/base.py", line 164, in get_response
    response = response.render()
  File "python_venv/lib/python3.4/site-packages/django/template/response.py", line 158, in render
    self.content = self.rendered_content
  File "python_venv/lib/python3.4/site-packages/rest_framework/response.py", line 71, in rendered_content
    ret = renderer.render(self.data, media_type, context)
  File "python_venv/lib/python3.4/site-packages/rest_framework/renderers.py", line 669, in render
    context = self.get_context(data, accepted_media_type, renderer_context)
  File "python_venv/lib/python3.4/site-packages/rest_framework/renderers.py", line 613, in get_context
    raw_data_post_form = self.get_raw_data_form(data, view, 'POST', request)
  File "python_venv/lib/python3.4/site-packages/rest_framework/renderers.py", line 561, in get_raw_data_form
    content = renderer.render(serializer.data, accepted, context)
  File "python_venv/src/django-rest-framework-json-api/rest_framework_json_api/renderers.py", line 85, in render
    json_api_data = utils.build_json_resource_obj(fields, data, resource_instance, resource_name)
  File "python_venv/src/django-rest-framework-json-api/rest_framework_json_api/utils.py", line 149, in build_json_resource_obj
    ('id', encoding.force_text(resource_instance.pk)),
AttributeError: 'NoneType' object has no attribute 'pk'
```

It's because DRF browsable emulates POST create and don't provide resource_instance which JSONRenderer expects.

This commit fixes it.



Discussion:
Thanks for this PR, I'll try to replicate this and see if it is not something with your (quite simple) implementation - I use the browsable API myself so i'm not sure where the issue is comming from.

Hmm I see, I was not using the jsonapi parser as the default one. I will however make some changes to your fix and submit a different PR - I'll let you know.


Resolving patch:
diff --git a/rest_framework_json_api/utils.py b/rest_framework_json_api/utils.py
--- a/rest_framework_json_api/utils.py
+++ b/rest_framework_json_api/utils.py
@@ -146,7 +146,7 @@ def format_relation_name(value, format_type=None):
 def build_json_resource_obj(fields, resource, resource_instance, resource_name):
     resource_data = [
         ('type', resource_name),
-        ('id', encoding.force_text(resource_instance.pk)),
+        ('id', encoding.force_text(resource_instance.pk) if resource_instance else None),
         ('attributes', extract_attributes(fields, resource)),
     ]
     relationships = extract_relationships(fields, resource, resource_instance)
@@ -204,6 +204,11 @@ def extract_attributes(fields, resource):
 
 def extract_relationships(fields, resource, resource_instance):
     data = OrderedDict()
+
+    # Don't try to extract relationships from a non-existent resource
+    if resource_instance is None:
+        return
+
     for field_name, field in six.iteritems(fields):
         # Skip URL field
         if field_name == api_settings.URL_FIELD_NAME:


Test patch: