Skip to content

Commit 3833271

Browse files
nattyg93sliverc
andauthored
Support many related fields on plain Serializers (#868)
Support many related fields on plain Serializers Co-authored-by: Oliver Sauder <[email protected]>
1 parent 3263061 commit 3833271

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

Diff for: CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
Note that in line with [Django REST Framework policy](http://www.django-rest-framework.org/topics/release-notes/),
99
any parts of the framework not mentioned in the documentation should generally be considered private API, and may be subject to change.
1010

11-
## [Unreleased] - TBD
11+
## [Unreleased]
1212

1313
### Added
1414

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

1919
* 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)
20+
* Correctly resolve the resource type of `ResourceRelatedField(many=True)` fields on plain serializers
2021

2122

2223
## [4.0.0] - 2020-10-31

Diff for: rest_framework_json_api/utils.py

+5
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,11 @@ def get_related_resource_type(relation):
215215
return get_related_resource_type(parent_model_relation)
216216

217217
if relation_model is None:
218+
# For ManyRelatedFields on plain Serializers the resource_type
219+
# cannot be determined from a model, so we must get it from the
220+
# child_relation
221+
if hasattr(relation, "child_relation"):
222+
return get_related_resource_type(relation.child_relation)
218223
raise APIException(
219224
_("Could not resolve resource type for relation %s" % relation)
220225
)

Diff for: tests/test_utils.py

+21
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,27 @@ class Meta:
243243
assert get_related_resource_type(field) == output
244244

245245

246+
@pytest.mark.parametrize(
247+
"related_field_kwargs,output",
248+
[
249+
({"queryset": BasicModel.objects}, "BasicModel"),
250+
({"queryset": BasicModel.objects, "model": BasicModel}, "BasicModel"),
251+
({"model": BasicModel, "read_only": True}, "BasicModel"),
252+
],
253+
)
254+
def test_get_related_resource_type_from_plain_serializer_class(
255+
related_field_kwargs, output
256+
):
257+
class PlainRelatedResourceTypeSerializer(serializers.Serializer):
258+
basic_models = serializers.ResourceRelatedField(
259+
many=True, **related_field_kwargs
260+
)
261+
262+
serializer = PlainRelatedResourceTypeSerializer()
263+
field = serializer.fields["basic_models"]
264+
assert get_related_resource_type(field) == output
265+
266+
246267
class ManyToManyTargetSerializer(serializers.ModelSerializer):
247268
class Meta:
248269
model = ManyToManyTarget

0 commit comments

Comments
 (0)