File tree Expand file tree Collapse file tree 3 files changed +8
-2
lines changed Expand file tree Collapse file tree 3 files changed +8
-2
lines changed Original file line number Diff line number Diff line change @@ -19,6 +19,8 @@ any parts of the framework not mentioned in the documentation should generally b
1919
2020* Fixed OpenAPI schema generation for ` Serializer ` when used inside another ` Serializer ` or as a child of ` ListField ` .
2121* ` ModelSerializer ` fields are now returned in the same order than DRF
22+ * Avoided that an empty attributes dict is rendered in case serializer does not
23+ provide any attribute fields.
2224
2325### Removed
2426
Original file line number Diff line number Diff line change @@ -126,7 +126,7 @@ class WriteonlyTestSerializer(serializers.ModelSerializer):
126126
127127 class Meta :
128128 model = Entry
129- fields = ("comments" , "rating" )
129+ fields = ("headline" , " comments" , "rating" )
130130
131131 class WriteOnlyDummyTestViewSet (views .ReadOnlyModelViewSet ):
132132 queryset = Entry .objects .all ()
@@ -136,6 +136,7 @@ class WriteOnlyDummyTestViewSet(views.ReadOnlyModelViewSet):
136136 result = json .loads (rendered .decode ())
137137
138138 assert "rating" not in result ["data" ]["attributes" ]
139+ assert "headline" in result ["data" ]["attributes" ]
139140 assert "relationships" not in result ["data" ]
140141
141142
@@ -153,6 +154,7 @@ class EmptyRelationshipViewSet(views.ReadOnlyModelViewSet):
153154
154155 rendered = render_dummy_test_serialized_view (EmptyRelationshipViewSet , Author ())
155156 result = json .loads (rendered .decode ())
157+ assert "attributes" not in result ["data" ]
156158 assert "relationships" in result ["data" ]
157159 assert "bio" in result ["data" ]["relationships" ]
158160 assert result ["data" ]["relationships" ]["bio" ] == {"data" : None }
Original file line number Diff line number Diff line change @@ -452,8 +452,10 @@ def build_json_resource_obj(
452452 resource_data = {
453453 "type" : resource_name ,
454454 "id" : utils .get_resource_id (resource_instance , resource ),
455- "attributes" : cls .extract_attributes (fields , resource ),
456455 }
456+ attributes = cls .extract_attributes (fields , resource )
457+ if attributes :
458+ resource_data ["attributes" ] = attributes
457459 relationships = cls .extract_relationships (fields , resource , resource_instance )
458460 if relationships :
459461 resource_data ["relationships" ] = relationships
You can’t perform that action at this time.
0 commit comments