Skip to content

Commit

Permalink
fix: issues discovered while using REST API
Browse files Browse the repository at this point in the history
Can't use only("pk") because of possible select_related clauses in the
Collections queryset.
  • Loading branch information
pomegranited committed Aug 28, 2024
1 parent 526835d commit 35629c3
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions openedx_learning/apps/authoring/collections/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
)
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 35629c3

Please sign in to comment.