Skip to content

Commit dd0bb97

Browse files
committed
Add support for source with attributes in extra_kwargs
1 parent 9e05aa5 commit dd0bb97

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

Diff for: rest_framework/serializers.py

+28-1
Original file line numberDiff line numberDiff line change
@@ -1117,9 +1117,36 @@ def get_fields(self):
11171117
if source == '*':
11181118
source = field_name
11191119

1120+
# Get the right model and info for source with attributes
1121+
source_attrs = source.split('.')
1122+
source_info = info
1123+
source_model = model
1124+
1125+
if len(source_attrs) > 1:
1126+
attr_info = info
1127+
attr_model = model
1128+
1129+
for attr in source_attrs[:-1]:
1130+
if attr not in attr_info.relations:
1131+
break
1132+
1133+
attr_model = attr_info.relations[attr].related_model
1134+
attr_info = model_meta.get_field_info(attr_model)
1135+
else:
1136+
attr = source_attrs[-1]
1137+
if (
1138+
attr in attr_info.fields_and_pk
1139+
or attr in attr_info.relations
1140+
or hasattr(attr_model, attr)
1141+
or attr == self.url_field_name
1142+
):
1143+
source = attr
1144+
source_info = attr_info
1145+
source_model = attr_model
1146+
11201147
# Determine the serializer field class and keyword arguments.
11211148
field_class, field_kwargs = self.build_field(
1122-
source, info, model, depth
1149+
source, source_info, source_model, depth
11231150
)
11241151

11251152
# Include any kwargs defined in `Meta.extra_kwargs`

0 commit comments

Comments
 (0)