Skip to content

Don't render write only relations #522

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 6 commits into from
Nov 29, 2018
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 @@ -3,6 +3,7 @@ Adam Ziolkowski <[email protected]>
Alan Crosswell <[email protected]>
Anton Shutik <[email protected]>
Christian Zosel <https://zosel.ch>
David Vogt <[email protected]>
Greg Aker <[email protected]>
Jamie Bliss <[email protected]>
Jerel Unruh <[email protected]>
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ any parts of the framework not mentioned in the documentation should generally b
* Pass context from `PolymorphicModelSerializer` to child serializers to support fields which require a `request` context such as `url`.
* Avoid patch on `RelationshipView` deleting relationship instance when constraint would allow null ([#242](https://github.com/django-json-api/django-rest-framework-json-api/issues/242))
* Avoid error with related urls when retrieving relationship which is referenced as `ForeignKey` on parent
* Do not render `write_only` relations


## [2.6.0] - 2018-09-20
Expand Down
32 changes: 31 additions & 1 deletion example/tests/unit/test_renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class ReadOnlyDummyTestViewSet(views.ReadOnlyModelViewSet):


def render_dummy_test_serialized_view(view_class):
serializer = DummyTestSerializer(instance=Entry())
serializer = view_class.serializer_class(instance=Entry())
renderer = JSONRenderer()
return renderer.render(
serializer.data,
Expand Down Expand Up @@ -87,3 +87,33 @@ def test_render_format_keys(settings):

result = json.loads(rendered.decode())
assert result['data']['attributes']['json-field'] == {'json-key': 'JsonValue'}


def test_writeonly_not_in_response(settings):
"""Test that writeonly fields are not shown in list response"""

settings.JSON_API_FORMAT_FIELD_NAMES = 'dasherize'

class WriteonlyTestSerializer(serializers.ModelSerializer):
'''Serializer for testing the absence of write_only fields'''
comments = serializers.ResourceRelatedField(
many=True,
write_only=True,
queryset=Comment.objects.all()
)

rating = serializers.IntegerField(write_only=True)

class Meta:
model = Entry
fields = ('comments', 'rating')

class WriteOnlyDummyTestViewSet(views.ReadOnlyModelViewSet):
queryset = Entry.objects.all()
serializer_class = WriteonlyTestSerializer

rendered = render_dummy_test_serialized_view(WriteOnlyDummyTestViewSet)
result = json.loads(rendered.decode())

assert 'rating' not in result['data']['attributes']
assert 'relationships' not in result['data']
4 changes: 4 additions & 0 deletions rest_framework_json_api/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ def extract_relationships(cls, fields, resource, resource_instance):
if field_name == api_settings.URL_FIELD_NAME:
continue

# don't output a key for write only fields
if fields[field_name].write_only:
continue

# Skip fields without relations
if not isinstance(
field, (relations.RelatedField, relations.ManyRelatedField, BaseSerializer)
Expand Down