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

chore: Replicate Django's signature at SoftDelete queryset #622

Merged
merged 1 commit into from
Aug 28, 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
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]
foarsitter marked this conversation as resolved.
Show resolved Hide resolved
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}
)
Loading