Skip to content

Commit

Permalink
Merge pull request #187 from ZEROFAIL/bugfix/implicit_fields2
Browse files Browse the repository at this point in the history
Fix crash related to Meta.fields / Meta.exlcude
  • Loading branch information
jerel committed Jan 4, 2016
2 parents 7699b98 + bb137f5 commit 1e2d7d9
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 9 deletions.
3 changes: 2 additions & 1 deletion example/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,5 @@ class CommentSerializer(serializers.ModelSerializer):

class Meta:
model = Comment
fields = ('entry', 'body', 'author',)
exclude = ('created_at', 'modified_at',)
# fields = ('entry', 'body', 'author',)
41 changes: 41 additions & 0 deletions example/tests/test_serializers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.core.urlresolvers import reverse
from django.test import TestCase
from django.utils import timezone

Expand All @@ -6,6 +7,11 @@

from example.models import Blog, Entry, Author

import pytest
from example.tests.utils import dump_json, redump_json

pytestmark = pytest.mark.django_db


class TestResourceIdentifierObjectSerializer(TestCase):
def setUp(self):
Expand Down Expand Up @@ -71,3 +77,38 @@ def test_deserialize_many(self):

print(serializer.data)


class TestModelSerializer(object):
def test_model_serializer_with_implicit_fields(self, comment, client):
expected = {
"data": {
"type": "comments",
"id": str(comment.pk),
"attributes": {
"body": comment.body
},
"relationships": {
"entry": {
"data": {
"type": "entries",
"id": str(comment.entry.pk)
}
},
"author": {
"data": {
"type": "authors",
"id": str(comment.author.pk)
}
},
}
}
}

response = client.get(reverse("comment-detail", kwargs={'pk': comment.pk}))

assert response.status_code == 200

actual = redump_json(response.content)
expected_json = dump_json(expected)

assert actual == expected_json
10 changes: 2 additions & 8 deletions rest_framework_json_api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,6 @@ class ModelSerializer(IncludedResourcesValidationMixin, SparseFieldsetsMixin, Mo
"""
serializer_related_field = ResourceRelatedField

def __init__(self, *args, **kwargs):
meta_fields = getattr(self.Meta, 'meta_fields', [])
# we add meta_fields to fields so they will be serialized like usual
self.Meta.fields = tuple(tuple(self.Meta.fields) + tuple(meta_fields))
super(ModelSerializer, self).__init__(*args, **kwargs)

def get_field_names(self, declared_fields, info):
"""
We override the parent to omit explicity defined meta fields (such
Expand All @@ -155,5 +149,5 @@ def get_field_names(self, declared_fields, info):
field = declared_fields[field_name]
if field_name not in meta_fields:
declared[field_name] = field
return super(ModelSerializer, self).get_field_names(declared, info)

fields = super(ModelSerializer, self).get_field_names(declared, info)
return list(fields) + list(getattr(self.Meta, 'meta_fields', list()))

0 comments on commit 1e2d7d9

Please sign in to comment.