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

Issue:
exception on import of the serializer
Dear,

my code:

```
from rest_framework_json_api import serializers
```

when using `from rest_framework import serializers` all is working.

the error when executing runserver and triggering a request:

```
Traceback (most recent call last):
  File "/usr/lib/python2.7/wsgiref/handlers.py", line 85, in run
    self.result = application(self.environ, self.start_response)
  File "/srv/www/vbnet/.ve/local/lib/python2.7/site-packages/django/contrib/staticfiles/handlers.py", line 64, in __call__
    return self.application(environ, start_response)
  File "/srv/www/vbnet/.ve/local/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 168, in __call__
    self.load_middleware()
  File "/srv/www/vbnet/.ve/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 46, in load_middleware
    mw_instance = mw_class()
  File "/srv/www/vbnet/.ve/local/lib/python2.7/site-packages/django/middleware/locale.py", line 23, in __init__
    for url_pattern in get_resolver(None).url_patterns:
  File "/srv/www/vbnet/.ve/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 372, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/srv/www/vbnet/.ve/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 366, in urlconf_module
    self._urlconf_module = import_module(self.urlconf_name)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/srv/www/vbnet/site/project/urls.py", line 13, in <module>
    from location.views import VenueDetailView, VenueListView, \
  File "/srv/www/vbnet/site/location/views.py", line 19, in <module>
    from services.rest.serializers import RatingSerializer
  File "/srv/www/vbnet/site/services/rest/serializers.py", line 7, in <module>
    from rest_framework_json_api import serializers
  File "/srv/www/vbnet/.ve/local/lib/python2.7/site-packages/rest_framework_json_api/serializers.py", line 5, in <module>
    from rest_framework_json_api.relations import ResourceRelatedField
  File "/srv/www/vbnet/.ve/local/lib/python2.7/site-packages/rest_framework_json_api/relations.py", line 7, in <module>
    from rest_framework_json_api.exceptions import Conflict
  File "/srv/www/vbnet/.ve/local/lib/python2.7/site-packages/rest_framework_json_api/exceptions.py", line 5, in <module>
    from rest_framework.views import exception_handler as drf_exception_handler
  File "/srv/www/vbnet/.ve/local/lib/python2.7/site-packages/rest_framework/views.py", line 98, in <module>
    class APIView(View):
  File "/srv/www/vbnet/.ve/local/lib/python2.7/site-packages/rest_framework/views.py", line 102, in APIView
    parser_classes = api_settings.DEFAULT_PARSER_CLASSES
  File "/srv/www/vbnet/.ve/local/lib/python2.7/site-packages/rest_framework/settings.py", line 208, in __getattr__
    val = perform_import(val, attr)
  File "/srv/www/vbnet/.ve/local/lib/python2.7/site-packages/rest_framework/settings.py", line 160, in perform_import
    return [import_from_string(item, setting_name) for item in val]
  File "/srv/www/vbnet/.ve/local/lib/python2.7/site-packages/rest_framework/settings.py", line 176, in import_from_string
    raise ImportError(msg)
ImportError: Could not import 'rest_framework_json_api.parsers.JSONParser' for API setting 'DEFAULT_PARSER_CLASSES'. ImportError: cannot import name exceptions.
```

I'm running your 2.0.0.beta.2



Discussion:
Do your settings look roughly like this? https://github.com/django-json-api/django-rest-framework-json-api/blob/develop/example/settings/dev.py#L39-L56 The error is basic enough that I'm guessing you have something misconfigured

Dear,

settings are c&p'd:

```
# -- REST
REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': ('rest_framework.permissions.IsAdminUser',),
    'PAGE_SIZE': 10,
    'PAGINATE_BY': 10,
    'PAGINATE_BY_PARAM': 'page_size',
    'MAX_PAGINATE_BY': 100,
    # DRF v3.1+
    'DEFAULT_PAGINATION_CLASS':
        'rest_framework_json_api.pagination.PageNumberPagination',
    'DEFAULT_PARSER_CLASSES': (
        'rest_framework_json_api.parsers.JSONParser',
        'rest_framework.parsers.FormParser',
        'rest_framework.parsers.MultiPartParser'
    ),
    'DEFAULT_RENDERER_CLASSES': (
        'rest_framework_json_api.renderers.JSONRenderer',
        'rest_framework.renderers.BrowsableAPIRenderer',
    ),
    #'DEFAULT_METADATA_CLASS': 'rest_framework_json_api.metadata.JSONAPIMetadata',
}

JSON_API_PLURALIZE_RELATION_TYPE = True
#JSON_API_FORMAT_KEYS = 'underscore'
JSON_API_FORMAT_RELATION_KEYS = 'underscore'
```

error smells like circular import, but I can't make sense of that.

found it:

```
File "/srv/www/vbnet/.ve/local/lib/python2.7/site-packages/rest_framework/views.py", line 102, in APIView
    parser_classes = api_settings.DEFAULT_PARSER_CLASSES
```

triggers the read of the parsers and its import. the parser class imports exceptions from . and  inside exceptions we are importing:

```
from rest_framework.views import exception_handler as drf_exception_handler
```

means it requires `drf.views` which hence would try to get the parser class again...

I changed the imports of the parsers testwise into:

```
  from rest_framework import parsers, exceptions
  from rest_framework.exceptions import ParseError

  from . import utils, renderers
```

and its working now.

dear, I wonder why it got closed. this is a error in the package as none of my code was involved. means, adopting your code won't make sense?

can I provide anything to support you?

I've run into this exact same issue. Why was this issue closed?

Dear, no answer since then. not sure if this package is properly maintained. feel free to see my fork. its working for me on production since december.

best

j

Yes it's maintained, I believe this issue was closed because we understood it to be resolved. If you have the fix in your fork would you be interested in opening a pull request?

Dear, I'm hesitating as I'm already late with a pull request for django-registration as it always involves proper code, tests, docu etc. I fear I cant' deliver proper quality to contribute or it will take a long time until its done.... 

I have a patch I'll push out shortly.


Resolving patch:
diff --git a/rest_framework_json_api/exceptions.py b/rest_framework_json_api/exceptions.py
--- a/rest_framework_json_api/exceptions.py
+++ b/rest_framework_json_api/exceptions.py
@@ -2,12 +2,18 @@
 from django.utils import six, encoding
 from django.utils.translation import ugettext_lazy as _
 from rest_framework import status, exceptions
-from rest_framework.views import exception_handler as drf_exception_handler
 
 from rest_framework_json_api.utils import format_value
 
 
 def exception_handler(exc, context):
+    # Import this here to avoid potential edge-case circular imports, which
+    # crashes with:
+    # "ImportError: Could not import 'rest_framework_json_api.parsers.JSONParser' for API setting
+    # 'DEFAULT_PARSER_CLASSES'. ImportError: cannot import name 'exceptions'.'"
+    #
+    # Also see: https://github.com/django-json-api/django-rest-framework-json-api/issues/158
+    from rest_framework.views import exception_handler as drf_exception_handler
     response = drf_exception_handler(exc, context)
 
     if not response:


Test patch: