From 35629c30d39cfb76a7fc2ff59d5e65e1bce080f9 Mon Sep 17 00:00:00 2001 From: Jillian Vogel Date: Wed, 28 Aug 2024 13:27:34 +0930 Subject: [PATCH] fix: issues discovered while using REST API Can't use only("pk") because of possible select_related clauses in the Collections queryset. --- openedx_learning/apps/authoring/collections/api.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/openedx_learning/apps/authoring/collections/api.py b/openedx_learning/apps/authoring/collections/api.py index 921ab292..464fd467 100644 --- a/openedx_learning/apps/authoring/collections/api.py +++ b/openedx_learning/apps/authoring/collections/api.py @@ -101,12 +101,13 @@ def add_to_collections( """ collection_objects = [] object_ids = contents_qset.values_list("pk", flat=True) + collection_ids = collections_qset.values_list("pk", flat=True) - for collection in collections_qset.only("pk").all(): + for collection_id in collection_ids: for object_id in object_ids: collection_objects.append( CollectionObject( - collection_id=collection.pk, + collection_id=collection_id, object_id=object_id, ) ) @@ -140,16 +141,17 @@ def remove_from_collections( """ total_deleted = 0 object_ids = contents_qset.values_list("pk", flat=True) + collection_ids = collections_qset.values_list("pk", flat=True) modified_collection_ids = [] - for collection in collections_qset.only("pk").all(): + for collection_id in collection_ids: num_deleted, _ = CollectionObject.objects.filter( - collection_id=collection.pk, + collection_id=collection_id, object_id__in=object_ids, ).delete() if num_deleted: - modified_collection_ids.append(collection.pk) + modified_collection_ids.append(collection_id) total_deleted += num_deleted