Skip to content
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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
Note that in line with [Django REST Framework policy](http://www.django-rest-framework.org/topics/release-notes/),
any parts of the framework not mentioned in the documentation should generally be considered private API, and may be subject to change.

## [Unreleased] - TBD
## [Unreleased]

### Added

Expand All @@ -17,6 +17,7 @@ any parts of the framework not mentioned in the documentation should generally b
### Fixed

* Allow users to overwrite a view's `get_serializer_class()` method when using [related urls](https://django-rest-framework-json-api.readthedocs.io/en/stable/usage.html#related-urls)
* Correctly resolve the resource type of `ResourceRelatedField(many=True)` fields on plain serializers


## [4.0.0] - 2020-10-31
Expand Down
5 changes: 5 additions & 0 deletions rest_framework_json_api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,11 @@ def get_related_resource_type(relation):
return get_related_resource_type(parent_model_relation)

if relation_model is None:
# For ManyRelatedFields on plain Serializers the resource_type
# cannot be determined from a model, so we must get it from the
# child_relation
if hasattr(relation, "child_relation"):
return get_related_resource_type(relation.child_relation)
raise APIException(
_("Could not resolve resource type for relation %s" % relation)
)
Expand Down
21 changes: 21 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,27 @@ class Meta:
assert get_related_resource_type(field) == output


@pytest.mark.parametrize(
"related_field_kwargs,output",
[
({"queryset": BasicModel.objects}, "BasicModel"),
({"queryset": BasicModel.objects, "model": BasicModel}, "BasicModel"),
({"model": BasicModel, "read_only": True}, "BasicModel"),
],
)
def test_get_related_resource_type_from_plain_serializer_class(
related_field_kwargs, output
):
class PlainRelatedResourceTypeSerializer(serializers.Serializer):
basic_models = serializers.ResourceRelatedField(
many=True, **related_field_kwargs
)

serializer = PlainRelatedResourceTypeSerializer()
field = serializer.fields["basic_models"]
assert get_related_resource_type(field) == output


class ManyToManyTargetSerializer(serializers.ModelSerializer):
class Meta:
model = ManyToManyTarget
Expand Down