Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add util to get the related objects to delete #347

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Loading