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

adding check for nullity in related models delete #243

Merged
merged 2 commits into from
Oct 14, 2024
Merged
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
6 changes: 4 additions & 2 deletions safedelete/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,10 @@ def soft_delete_cascade_policy_action(self, **kwargs) -> Tuple[int, Dict[str, in
deleted_counter: Counter = Counter()
for related in related_objects(self):
if is_safedelete_cls(related.__class__) and not getattr(related, FIELD_NAME):
_, delete_response = related.delete(force_policy=SOFT_DELETE, is_cascade=True, **kwargs)
deleted_counter.update(delete_response)
res = related.delete(force_policy=SOFT_DELETE, is_cascade=True, **kwargs)
if res is not None:
_, delete_response = res
deleted_counter.update(delete_response)

# soft-delete the object
_, delete_response = self._delete(force_policy=SOFT_DELETE, **kwargs)
Expand Down
6 changes: 4 additions & 2 deletions safedelete/queryset.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,10 @@ def delete(self, force_policy: Optional[int] = None) -> Tuple[int, Dict[str, int
deleted_counter: Counter = Counter()
# TODO: Replace this by bulk update if we can
for obj in self.all():
_, delete_response = obj.delete(force_policy=force_policy)
deleted_counter.update(delete_response)
res = obj.delete(force_policy=force_policy)
if res is not None:
_, delete_response = res
deleted_counter.update(delete_response)
self._result_cache = None
return sum(deleted_counter.values()), dict(deleted_counter)
delete.alters_data = True # type: ignore
Expand Down