Skip to content

Commit

Permalink
chore: Replicate Django's signature at SoftDelete queryset
Browse files Browse the repository at this point in the history
  • Loading branch information
gmcrocetti committed Aug 28, 2024
1 parent 2882614 commit 1673fea
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ To be released
- Make `soft` argument to `SoftDeletableModel.delete()` keyword-only
- `JoinManager` and `JoinManagerMixin` have been deprecated;
please use ``JoinQueryset.as_manager()`` instead
- Change `SoftDeletableQuerySetMixin.delete` to replicate Django's API.

4.5.1 (2024-05-02)
------------------
Expand Down
10 changes: 5 additions & 5 deletions model_utils/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,17 +363,17 @@ class SoftDeletableQuerySetMixin(Generic[ModelT]):
its ``is_removed`` field to True.
"""

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


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


Expand Down
10 changes: 10 additions & 0 deletions tests/test_models/test_softdeletable_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,13 @@ def test_instance_purge_no_connection(self) -> None:

def test_deprecation_warning(self) -> None:
self.assertWarns(DeprecationWarning, SoftDeletable.objects.all)

def test_delete_queryset_return(self) -> None:
SoftDeletable.available_objects.create(name='a')
SoftDeletable.available_objects.create(name='b')

result = SoftDeletable.available_objects.filter(name="a").delete()

assert result == (
1, {SoftDeletable._meta.label: 1}
)

0 comments on commit 1673fea

Please sign in to comment.