Skip to content

Fix for apps that don't load contenttypes module. #345

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ v2.3.0
* When `JSON_API_FORMAT_KEYS` is False (the default) do not translate request
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`.

v2.2.0

Expand Down
15 changes: 11 additions & 4 deletions rest_framework_json_api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,28 @@
try:
from rest_framework.serializers import ManyRelatedField
except ImportError:
ManyRelatedField = type(None)
ManyRelatedField = object()

try:
from rest_framework_nested.relations import HyperlinkedRouterField
except ImportError:
HyperlinkedRouterField = type(None)
HyperlinkedRouterField = object()

if django.VERSION >= (1, 9):
from django.db.models.fields.related_descriptors import ManyToManyDescriptor, ReverseManyToOneDescriptor
ReverseManyRelatedObjectsDescriptor = type(None)
from django.contrib.contenttypes.fields import ReverseGenericManyToOneDescriptor
ReverseManyRelatedObjectsDescriptor = object()
else:
from django.db.models.fields.related import ManyRelatedObjectsDescriptor as ManyToManyDescriptor
from django.db.models.fields.related import ForeignRelatedObjectsDescriptor as ReverseManyToOneDescriptor
from django.db.models.fields.related import ReverseManyRelatedObjectsDescriptor

# Generic relation descriptor from django.contrib.contenttypes.
if 'django.contrib.contenttypes' not in settings.INSTALLED_APPS: # pragma: no cover
# Target application does not use contenttypes. Importing would cause errors.
ReverseGenericManyToOneDescriptor = object()
elif django.VERSION >= (1, 9):
from django.contrib.contenttypes.fields import ReverseGenericManyToOneDescriptor
else:
from django.contrib.contenttypes.fields import ReverseGenericRelatedObjectsDescriptor as ReverseGenericManyToOneDescriptor


Expand Down