Skip to content

Commit b844078

Browse files
committed
Do not apply filter to nested serializers
1 parent 806e260 commit b844078

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

drf_dynamic_fields/__init__.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,22 @@ def fields(self):
1515
"""
1616
Filters the fields according to the `fields` query parameter.
1717
18-
a blank `fields` parameter (?fields) will remove all fields.
19-
not passing `fields` will pass all fields
20-
individual fields are comma separated (?fields=id,name,url,email)
18+
A blank `fields` parameter (?fields) will remove all fields. Not
19+
passing `fields` will pass all fields individual fields are comma
20+
separated (?fields=id,name,url,email).
21+
2122
"""
2223
fields = super(DynamicFieldsMixin, self).fields
2324

2425
if not hasattr(self, '_context'):
25-
# we are being called before a request cycle.
26+
# We are being called before a request cycle
27+
return fields
28+
29+
# Only filter if this is the root serializer, or if the parent is the
30+
# root serializer with many=True
31+
is_root = self.root == self
32+
parent_is_list_root = self.parent == self.root and getattr(self.parent, 'many', False)
33+
if not (is_root or parent_is_list_root):
2634
return fields
2735

2836
try:

0 commit comments

Comments
 (0)