Skip to content

Commit 7b091f5

Browse files
authored
Merge pull request #622 from gmcrocetti/issue-541
chore: Replicate Django's signature at SoftDelete queryset
2 parents 2882614 + 1673fea commit 7b091f5

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

CHANGES.rst

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ To be released
1616
- Make `soft` argument to `SoftDeletableModel.delete()` keyword-only
1717
- `JoinManager` and `JoinManagerMixin` have been deprecated;
1818
please use ``JoinQueryset.as_manager()`` instead
19+
- Change `SoftDeletableQuerySetMixin.delete` to replicate Django's API.
1920

2021
4.5.1 (2024-05-02)
2122
------------------

model_utils/managers.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -363,17 +363,17 @@ class SoftDeletableQuerySetMixin(Generic[ModelT]):
363363
its ``is_removed`` field to True.
364364
"""
365365

366-
def delete(self) -> None:
366+
def delete(self) -> tuple[int, dict[str, int]]:
367367
"""
368368
Soft delete objects from queryset (set their ``is_removed``
369369
field to True)
370370
"""
371-
cast(QuerySet[ModelT], self).update(is_removed=True)
371+
model: type[ModelT] = self.model # type: ignore[attr-defined]
372+
number_of_deleted_objects = cast(QuerySet[ModelT], self).update(is_removed=True)
373+
return number_of_deleted_objects, {model._meta.label: number_of_deleted_objects}
372374

373375

374-
# Note that our delete() method does not return anything, unlike Django's.
375-
# https://github.com/jazzband/django-model-utils/issues/541
376-
class SoftDeletableQuerySet(SoftDeletableQuerySetMixin[ModelT], QuerySet[ModelT]): # type: ignore[misc]
376+
class SoftDeletableQuerySet(SoftDeletableQuerySetMixin[ModelT], QuerySet[ModelT]):
377377
pass
378378

379379

tests/test_models/test_softdeletable_model.py

+10
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,13 @@ def test_instance_purge_no_connection(self) -> None:
5353

5454
def test_deprecation_warning(self) -> None:
5555
self.assertWarns(DeprecationWarning, SoftDeletable.objects.all)
56+
57+
def test_delete_queryset_return(self) -> None:
58+
SoftDeletable.available_objects.create(name='a')
59+
SoftDeletable.available_objects.create(name='b')
60+
61+
result = SoftDeletable.available_objects.filter(name="a").delete()
62+
63+
assert result == (
64+
1, {SoftDeletable._meta.label: 1}
65+
)

0 commit comments

Comments
 (0)