File tree Expand file tree Collapse file tree 2 files changed +18
-6
lines changed Expand file tree Collapse file tree 2 files changed +18
-6
lines changed Original file line number Diff line number Diff line change @@ -42,12 +42,29 @@ class Meta:
42
42
fields = ('name' , 'data' )
43
43
44
44
45
+ class ListBumpedThreadSerializer (ListSerializer ):
46
+ """Custom ListSerializer to override to_representation() when list views are triggered."""
47
+
48
+ def to_representation (self , objects : list [BumpedThread ]) -> int :
49
+ """
50
+ Used by the `ListModelMixin` to return just the list of bumped thread ids.
51
+
52
+ We want to only return the thread_id attribute, hence it is unnecessary
53
+ to create a nested dictionary.
54
+
55
+ Additionally, this allows bumped thread routes to simply return an
56
+ array of thread_id ints instead of objects, saving on bandwidth.
57
+ """
58
+ return [obj .thread_id for obj in objects ]
59
+
60
+
45
61
class BumpedThreadSerializer (ModelSerializer ):
46
62
"""A class providing (de-)serialization of `BumpedThread` instances."""
47
63
48
64
class Meta :
49
65
"""Metadata defined for the Django REST Framework."""
50
66
67
+ list_serializer_class = ListBumpedThreadSerializer
51
68
model = BumpedThread
52
69
fields = ('thread_id' ,)
53
70
Original file line number Diff line number Diff line change @@ -18,12 +18,7 @@ class BumpedThreadViewSet(
18
18
Returns all BumpedThread items in the database.
19
19
20
20
#### Response format
21
- >>> [
22
- ... {
23
- ... 'thread_id': "941705627405811793",
24
- ... },
25
- ... ...
26
- ... ]
21
+ >>> list[int]
27
22
28
23
#### Status codes
29
24
- 200: returned on success
You can’t perform that action at this time.
0 commit comments