From bbe1f6364bc392a453f3886f51f7b84e1d275dfe Mon Sep 17 00:00:00 2001 From: FiveBoroughs Date: Sun, 16 Nov 2025 23:29:17 +0100 Subject: [PATCH] Fix: Preserve stream order in ChannelSerializer PATCH/PUT responses The ChannelSerializer.to_representation() method was not respecting the ChannelStream.order field when serializing PATCH/PUT responses. This caused streams to be returned in an arbitrary order rather than the order specified in the request. The update() method correctly saves the stream order to the database using the ChannelStream.order field, and GET requests (with include_streams=True) correctly return ordered streams via get_streams(). However, standard PATCH/PUT responses were using PrimaryKeyRelatedField which doesn't respect the ordering. This fix ensures that all representations (GET, PATCH, PUT) return streams ordered by the channelstream__order field. Impact: - PATCH/PUT responses now correctly reflect the stream order saved - Clients can trust the response data without needing a follow-up GET - No breaking changes - only fixes inconsistent behavior Tested with: - PATCH request with ordered stream IDs - Verified response matches request order - Verified GET request confirms order persisted to database --- apps/channels/serializers.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/apps/channels/serializers.py b/apps/channels/serializers.py index 7058ced2..264a45ea 100644 --- a/apps/channels/serializers.py +++ b/apps/channels/serializers.py @@ -348,8 +348,17 @@ class ChannelSerializer(serializers.ModelSerializer): if include_streams: self.fields["streams"] = serializers.SerializerMethodField() - - return super().to_representation(instance) + return super().to_representation(instance) + else: + # Fix: For PATCH/PUT responses, ensure streams are ordered + representation = super().to_representation(instance) + if "streams" in representation: + representation["streams"] = list( + instance.streams.all() + .order_by("channelstream__order") + .values_list("id", flat=True) + ) + return representation def get_logo(self, obj): return LogoSerializer(obj.logo).data