Skip to content

Commit

Permalink
Fix SafeDeleteQueryset.as_manager to return SafeDeleteManager (#228)
Browse files Browse the repository at this point in the history
Calling the as_manager method on a SafeDeleteQueryset subclass previously
created a SafeDelete naive manager. This commit overrides the as_manager
method to properly return a SafeDeleteManager object.
  • Loading branch information
gwax authored Mar 30, 2023
1 parent 179288b commit ac2a6a5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ CHANGELOG
- Fix de translation
- Fix argument type passed to db_for_write #222
- Django 4.2 support #223
- Fix SafeDeleteQueryset.as_manager() now returns SafeDeleteManager

1.3.1 (2022-10-03)
==================
Expand Down
12 changes: 12 additions & 0 deletions safedelete/queryset.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ def __init__(
super(SafeDeleteQueryset, self).__init__(model=model, query=query, using=using, hints=hints)
self.query: SafeDeleteQuery = query or SafeDeleteQuery(self.model)

@classmethod
def as_manager(cls):
"""Override as_manager behavior to ensure we create a SafeDeleteManager.
"""
# Address the circular dependency between `SafeDeleteQueryset` and `SafeDeleteManager`.
from .managers import SafeDeleteManager

manager = SafeDeleteManager.from_queryset(cls)()
manager._built_with_as_manager = True
return manager
as_manager.queryset_only = True # type: ignore

def delete(self, force_policy: Optional[int] = None) -> Tuple[int, Dict[str, int]]:
"""Overrides bulk delete behaviour.
Expand Down
7 changes: 7 additions & 0 deletions safedelete/tests/test_custom_queryset.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ def test_custom_queryset_without_manager(self):
# and they can be custom filtered
self.assertEqual(deleted_only.green().count(), 1)

def test_custom_queryset_as_manager_is_safedelete(self):
"""Test whether SafeDeleteQueryset as_manager works as intended
"""
manager = CustomQuerySet.as_manager()
self.assertIsInstance(manager, SafeDeleteManager)
self.assertFalse(hasattr(manager, 'as_manager'))

@staticmethod
def _create_green_instance():
"""Shortcut for creating instance with ``color == green``
Expand Down

0 comments on commit ac2a6a5

Please sign in to comment.