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