File tree 3 files changed +8
-2
lines changed
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
19
19
20
20
* Fixed OpenAPI schema generation for ` Serializer ` when used inside another ` Serializer ` or as a child of ` ListField ` .
21
21
* ` 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.
22
24
23
25
### Removed
24
26
Original file line number Diff line number Diff line change @@ -126,7 +126,7 @@ class WriteonlyTestSerializer(serializers.ModelSerializer):
126
126
127
127
class Meta :
128
128
model = Entry
129
- fields = ("comments" , "rating" )
129
+ fields = ("headline" , " comments" , "rating" )
130
130
131
131
class WriteOnlyDummyTestViewSet (views .ReadOnlyModelViewSet ):
132
132
queryset = Entry .objects .all ()
@@ -136,6 +136,7 @@ class WriteOnlyDummyTestViewSet(views.ReadOnlyModelViewSet):
136
136
result = json .loads (rendered .decode ())
137
137
138
138
assert "rating" not in result ["data" ]["attributes" ]
139
+ assert "headline" in result ["data" ]["attributes" ]
139
140
assert "relationships" not in result ["data" ]
140
141
141
142
@@ -153,6 +154,7 @@ class EmptyRelationshipViewSet(views.ReadOnlyModelViewSet):
153
154
154
155
rendered = render_dummy_test_serialized_view (EmptyRelationshipViewSet , Author ())
155
156
result = json .loads (rendered .decode ())
157
+ assert "attributes" not in result ["data" ]
156
158
assert "relationships" in result ["data" ]
157
159
assert "bio" in result ["data" ]["relationships" ]
158
160
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(
452
452
resource_data = {
453
453
"type" : resource_name ,
454
454
"id" : utils .get_resource_id (resource_instance , resource ),
455
- "attributes" : cls .extract_attributes (fields , resource ),
456
455
}
456
+ attributes = cls .extract_attributes (fields , resource )
457
+ if attributes :
458
+ resource_data ["attributes" ] = attributes
457
459
relationships = cls .extract_relationships (fields , resource , resource_instance )
458
460
if relationships :
459
461
resource_data ["relationships" ] = relationships
You can’t perform that action at this time.
0 commit comments