File tree Expand file tree Collapse file tree 1 file changed +8
-4
lines changed Expand file tree Collapse file tree 1 file changed +8
-4
lines changed Original file line number Diff line number Diff line change @@ -239,10 +239,14 @@ def extract_attributes(fields, resource):
239239 if isinstance (field , (RelatedField , BaseSerializer , ManyRelatedField )):
240240 continue
241241
242- # Skip read_only attribute fields when the resource is non-existent
243- # Needed for the "Raw data" form of the browseable API
244- if resource .get (field_name ) is None and fields [field_name ].read_only :
245- continue
242+ # Skip read_only attribute fields when `resource` is an empty
243+ # serializer. Prevents the "Raw Data" form of the browseable API
244+ # from rendering `"foo": null` for read only fields
245+ try :
246+ resource [field_name ]
247+ except KeyError :
248+ if fields [field_name ].read_only :
249+ continue
246250
247251 data .update ({
248252 field_name : resource .get (field_name )
You can’t perform that action at this time.
0 commit comments