mirror of
https://github.com/Hopiu/django-model-utils.git
synced 2026-03-16 20:00:23 +00:00
chore: Replicate Django's signature at SoftDelete queryset
This commit is contained in:
parent
2882614f0c
commit
1673feae7b
3 changed files with 16 additions and 5 deletions
|
|
@ -16,6 +16,7 @@ To be released
|
||||||
- Make `soft` argument to `SoftDeletableModel.delete()` keyword-only
|
- Make `soft` argument to `SoftDeletableModel.delete()` keyword-only
|
||||||
- `JoinManager` and `JoinManagerMixin` have been deprecated;
|
- `JoinManager` and `JoinManagerMixin` have been deprecated;
|
||||||
please use ``JoinQueryset.as_manager()`` instead
|
please use ``JoinQueryset.as_manager()`` instead
|
||||||
|
- Change `SoftDeletableQuerySetMixin.delete` to replicate Django's API.
|
||||||
|
|
||||||
4.5.1 (2024-05-02)
|
4.5.1 (2024-05-02)
|
||||||
------------------
|
------------------
|
||||||
|
|
|
||||||
|
|
@ -363,17 +363,17 @@ class SoftDeletableQuerySetMixin(Generic[ModelT]):
|
||||||
its ``is_removed`` field to True.
|
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``
|
Soft delete objects from queryset (set their ``is_removed``
|
||||||
field to True)
|
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.
|
class SoftDeletableQuerySet(SoftDeletableQuerySetMixin[ModelT], QuerySet[ModelT]):
|
||||||
# https://github.com/jazzband/django-model-utils/issues/541
|
|
||||||
class SoftDeletableQuerySet(SoftDeletableQuerySetMixin[ModelT], QuerySet[ModelT]): # type: ignore[misc]
|
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -53,3 +53,13 @@ class SoftDeletableModelTests(TestCase):
|
||||||
|
|
||||||
def test_deprecation_warning(self) -> None:
|
def test_deprecation_warning(self) -> None:
|
||||||
self.assertWarns(DeprecationWarning, SoftDeletable.objects.all)
|
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…
Reference in a new issue