Skip to content

Commit

Permalink
Add util to get the related objects to delete
Browse files Browse the repository at this point in the history
  • Loading branch information
viggo-devries committed Apr 18, 2024
1 parent c89783e commit f303478
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
10 changes: 9 additions & 1 deletion oscarapi/serializers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@

import oscar.models.fields

from oscarapi.utils.loading import get_api_class
from oscarapi.utils.exists import construct_id_filter
from .fields import ImageUrlField

get_related_objects_to_delete = get_api_class(
"utils.exists", "get_related_objects_to_delete"
)

logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -57,7 +62,10 @@ def update_relation(self, name, manager, values):
# the product class changing for example, lots of attributes would become
# obsolete.
current_pks = [p.pk for p in updated_values]
manager.exclude(pk__in=current_pks).delete()
related_objects_to_delete = get_related_objects_to_delete(
name, manager, current_pks
)
related_objects_to_delete.delete()
else:
manager.set(updated_values)

Expand Down
5 changes: 5 additions & 0 deletions oscarapi/utils/exists.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,8 @@ def bound_unique_together_get_or_create(bound_queryset, datum):

def bound_unique_together_get_or_create_multiple(bound_queryset, data):
return [bound_unique_together_get_or_create(bound_queryset, date) for date in data]


# pylint: disable=W0613
def get_related_objects_to_delete(name, manager, current_pks):
return manager.exclude(pk__in=current_pks)

0 comments on commit f303478

Please sign in to comment.