Skip to content

Fixes types in reverse m2m relationships #355

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 4 commits into from
Jun 2, 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 AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ Greg Aker <greg@gregaker.net>
Jerel Unruh <mail@unruhdesigns.com>
Matt Layman <http://www.mattlayman.com>
Oliver Sauder <os@esite.ch>
Raphael Cohen <raphael.cohen.utt@gmail.com>
Yaniv Peer <yanivpeer@gmail.com>
2 changes: 1 addition & 1 deletion example/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class Entry(BaseModel):
body_text = models.TextField(null=True)
pub_date = models.DateField(null=True)
mod_date = models.DateField(null=True)
authors = models.ManyToManyField(Author)
authors = models.ManyToManyField(Author, related_name='entries')
n_comments = models.IntegerField(default=0)
n_pingbacks = models.IntegerField(default=0)
rating = models.IntegerField(default=0)
Expand Down
4 changes: 2 additions & 2 deletions example/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class AuthorBioSerializer(serializers.ModelSerializer):

class Meta:
model = AuthorBio
fields = ('author', 'body',)
fields = ('author', 'body')


class AuthorSerializer(serializers.ModelSerializer):
Expand All @@ -102,7 +102,7 @@ class AuthorSerializer(serializers.ModelSerializer):

class Meta:
model = Author
fields = ('name', 'email', 'bio')
fields = ('name', 'email', 'bio', 'entries')


class CommentSerializer(serializers.ModelSerializer):
Expand Down
11 changes: 10 additions & 1 deletion example/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""
from rest_framework_json_api import utils

from ..serializers import EntrySerializer
from ..serializers import EntrySerializer, AuthorSerializer
from ..tests import TestBase


Expand All @@ -29,3 +29,12 @@ def test_m2m_relation(self):
field = serializer.fields['authors']

self.assertEqual(utils.get_related_resource_type(field), 'authors')

def test_m2m_reverse_relation(self):
"""
Ensure reverse m2ms have their types identified correctly.
"""
serializer = AuthorSerializer()
field = serializer.fields['entries']

self.assertEqual(utils.get_related_resource_type(field), 'entries')
11 changes: 10 additions & 1 deletion rest_framework_json_api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,16 @@ def get_related_resource_type(relation):
else:
relation_model = parent_model_relation.related.model
elif parent_model_relation_type is ManyToManyDescriptor:
relation_model = parent_model_relation.field.remote_field.model
if django.VERSION >= (1, 9):
relation_model = parent_model_relation.field.remote_field.model
# In case we are in a reverse relation
if relation_model == parent_model:
relation_model = parent_model_relation.field.model
elif django.VERSION >= (1, 8):
relation_model = parent_model_relation.related.model
# In case we are in a reverse relation
if relation_model == parent_model:
relation_model = parent_model_relation.related.related_model
elif parent_model_relation_type is ReverseManyRelatedObjectsDescriptor:
relation_model = parent_model_relation.field.related.model
elif parent_model_relation_type is ReverseGenericManyToOneDescriptor:
Expand Down