Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0122469

Browse files
committed
Adding tests and cleanup
1 parent d2a236b commit 0122469

File tree

4 files changed

+67
-14
lines changed

4 files changed

+67
-14
lines changed

‎AUTHORS‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ Adam Wróbel <https://adamwrobel.com>
22
Adam Ziolkowski <adam@adsized.com>
33
Alan Crosswell <alan@columbia.edu>
44
Anton Shutik <shutikanton@gmail.com>
5+
Ashley Loewen <github@ashleycodes.tech>
56
Asif Saif Uddin <auvipy@gmail.com>
67
Beni Keller <beni@matraxi.ch>
78
Boris Pleshakov <koordinator.kun@gmail.com>

‎rest_framework_json_api/utils.py‎

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -211,21 +211,15 @@ def get_related_resource_type(relation):
211211
relation_model = relation.model
212212
elif hasattr(relation, "get_queryset") and relation.get_queryset() is not None:
213213
relation_model = relation.get_queryset().model
214-
elif (
215-
getattr(relation, "many", False)
216-
and hasattr(relation.child, "Meta")
217-
and hasattr(relation.child.Meta, "model")
218-
):
219-
# For ManyToMany relationships, get the model from the child
220-
# serializer of the list serializer
221-
relation_model = relation.child.Meta.model
222-
elif (
223-
hasattr(relation, "child_relation")
224-
and hasattr(relation.child_relation, "model")
225-
):
214+
elif hasattr(relation, "child_relation"):
226215
# For ManyRelatedField relationships, get the model from the child relationship
227-
relation_model = relation.child_relation.model
228-
else:
216+
try:
217+
return get_related_resource_type(relation.child_relation)
218+
except AttributeError:
219+
# Some read only relationships fail to get it directly, fall through to
220+
# get via the parent
221+
pass
222+
if not relation_model:
229223
parent_serializer = relation.parent
230224
parent_model = None
231225
if isinstance(parent_serializer, PolymorphicModelSerializer):

‎tests/models.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,14 @@ class ForeignKeySource(DJAModel):
3939
target = models.ForeignKey(
4040
ForeignKeyTarget, related_name="sources", on_delete=models.CASCADE
4141
)
42+
43+
44+
class NestedRelationshipSource(DJAModel):
45+
m2m_source = models.ManyToManyField(ManyToManySource, related_name="nested_source")
46+
fk_source = models.ForeignKey(
47+
ForeignKeySource, related_name="nested_source", on_delete=models.CASCADE
48+
)
49+
m2m_target = models.ManyToManyField(ManyToManySource, related_name="nested_source")
50+
fk_target = models.ForeignKey(
51+
ForeignKeySource, related_name="nested_source", on_delete=models.CASCADE
52+
)

‎tests/test_utils.py‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
ForeignKeyTarget,
2626
ManyToManySource,
2727
ManyToManyTarget,
28+
NestedRelationshipSource,
2829
)
2930
from tests.serializers import BasicModelSerializer
3031

@@ -313,6 +314,52 @@ class Meta:
313314
assert get_related_resource_type(field) == output
314315

315316

317+
@pytest.mark.parametrize(
318+
"model_class,field,output,related_field_kwargs",
319+
[
320+
(
321+
NestedRelationshipSource,
322+
"m2m_source.targets",
323+
"ManyToManyTarget",
324+
{"many": True, "queryset": ManyToManyTarget.objects.all()},
325+
),
326+
(
327+
NestedRelationshipSource,
328+
"m2m_target.sources.",
329+
"ManyToManySource",
330+
{"many": True, "queryset": ManyToManySource.objects.all()},
331+
),
332+
(
333+
NestedRelationshipSource,
334+
"fk_source.target",
335+
"ForeignKeyTarget",
336+
{"many": True, "queryset": ForeignKeyTarget.objects.all()},
337+
),
338+
(
339+
NestedRelationshipSource,
340+
"fk_target.source",
341+
"ForeignKeySource",
342+
{"many": True, "queryset": ForeignKeySource.objects.all()},
343+
),
344+
],
345+
)
346+
def test_get_related_resource_type_nested_source(
347+
model_class, field, output, related_field_kwargs
348+
):
349+
class RelatedResourceTypeSerializer(serializers.ModelSerializer):
350+
relationship = serializers.ResourceRelatedField(
351+
source=field, **related_field_kwargs
352+
)
353+
354+
class Meta:
355+
model = model_class
356+
fields = ("relationship",)
357+
358+
serializer = RelatedResourceTypeSerializer()
359+
field = serializer.fields["relationship"]
360+
assert get_related_resource_type(field) == output
361+
362+
316363
@pytest.mark.parametrize(
317364
"related_field_kwargs,output",
318365
[

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /