Skip to content

Commit

Permalink
adding check for nullity in related models delete (#243)
Browse files Browse the repository at this point in the history
* adding check for nullity in related models delete

* adding check for nullity in queryset delete
  • Loading branch information
AllenWang314 authored Oct 14, 2024
1 parent fd3f94e commit 7e03ff2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
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

0 comments on commit 7e03ff2

Please sign in to comment.