diff --git a/apps/channels/api_views.py b/apps/channels/api_views.py index 10d855db..3e786ff1 100644 --- a/apps/channels/api_views.py +++ b/apps/channels/api_views.py @@ -797,17 +797,21 @@ class ChannelViewSet(viewsets.ModelViewSet): if to_remove: channel.channelstream_set.filter(stream_id__in=to_remove).delete() + to_update = [] for order, stream_id in enumerate(normalized_ids): if stream_id in current_links: cs = current_links[stream_id] if cs.order != order: cs.order = order - cs.save(update_fields=["order"]) + to_update.append(cs) else: ChannelStream.objects.create( channel=channel, stream_id=stream_id, order=order ) + if to_update: + ChannelStream.objects.bulk_update(to_update, ["order"]) + # Return the updated objects (already in memory) serialized_channels = ChannelSerializer( [channel for channel, _ in validated_updates], diff --git a/apps/channels/serializers.py b/apps/channels/serializers.py index abf26e91..039386a3 100644 --- a/apps/channels/serializers.py +++ b/apps/channels/serializers.py @@ -365,7 +365,6 @@ class ChannelSerializer(serializers.ModelSerializer): normalized_ids = [ stream.id if hasattr(stream, "id") else stream for stream in streams ] - print(normalized_ids) # Get current mapping of stream_id -> ChannelStream current_links = { @@ -382,17 +381,21 @@ class ChannelSerializer(serializers.ModelSerializer): instance.channelstream_set.filter(stream_id__in=to_remove).delete() # Update or create with new order + to_update = [] for order, stream_id in enumerate(normalized_ids): if stream_id in current_links: cs = current_links[stream_id] if cs.order != order: cs.order = order - cs.save(update_fields=["order"]) + to_update.append(cs) else: ChannelStream.objects.create( channel=instance, stream_id=stream_id, order=order ) + if to_update: + ChannelStream.objects.bulk_update(to_update, ["order"]) + return instance def validate_channel_number(self, value):