diff --git a/CHANGELOG.md b/CHANGELOG.md index f68ebeba..6e2f731a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ any parts of the framework not mentioned in the documentation should generally b * Avoid exception when trying to include skipped relationship * Don't swallow `filter[]` params when there are several * Fix DeprecationWarning regarding collections.abc import in Python 3.7 +* Allow OPTIONS request to be used on RelationshipView. ## [2.7.0] - 2019-01-14 diff --git a/example/tests/test_views.py b/example/tests/test_views.py index 95fc92a3..16d0e4e1 100644 --- a/example/tests/test_views.py +++ b/example/tests/test_views.py @@ -274,6 +274,39 @@ def test_new_comment_data_patch_to_many_relationship(self): assert Comment.objects.filter(id=self.second_comment.id).exists() + def test_options_entry_relationship_blog(self): + url = reverse( + 'entry-relationships', kwargs={'pk': self.first_entry.id, 'related_field': 'blog'} + ) + response = self.client.options(url) + expected_data = { + "data": { + "name": "Entry Relationship", + "description": "", + "renders": [ + "application/vnd.api+json", + "text/html" + ], + "parses": [ + "application/vnd.api+json", + "application/x-www-form-urlencoded", + "multipart/form-data" + ], + "allowed_methods": [ + "GET", + "POST", + "PATCH", + "DELETE", + "HEAD", + "OPTIONS" + ], + "actions": { + "POST": {} + } + } + } + assert response.json() == expected_data + class TestRelatedMixin(APITestCase): diff --git a/rest_framework_json_api/serializers.py b/rest_framework_json_api/serializers.py index e1fe3d9f..9bfa0f62 100644 --- a/rest_framework_json_api/serializers.py +++ b/rest_framework_json_api/serializers.py @@ -29,10 +29,8 @@ class ResourceIdentifierObjectSerializer(BaseSerializer): def __init__(self, *args, **kwargs): self.model_class = kwargs.pop('model_class', self.model_class) - if 'instance' not in kwargs and not self.model_class: - raise RuntimeError( - 'ResourceIdentifierObjectsSerializer must be initialized with a model class.' - ) + # this has no fields but assumptions are made elsewhere that self.fields exists. + self.fields = {} super(ResourceIdentifierObjectSerializer, self).__init__(*args, **kwargs) def to_representation(self, instance):