Skip to content

Commit ba9ac5d

Browse files
authored
Merge pull request jazzband#612 from ProtixIT/generate-custom-soft-delete-manager
Auto-generate manager implementation for `CustomSoftDelete`
2 parents 0693983 + 512d0f1 commit ba9ac5d

File tree

3 files changed

+19
-22
lines changed

3 files changed

+19
-22
lines changed

tests/managers.py

-13
This file was deleted.

tests/models.py

+13-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@
99

1010
from model_utils import Choices
1111
from model_utils.fields import MonitorField, SplitField, StatusField, UUIDField
12-
from model_utils.managers import InheritanceManager, JoinManager, QueryManager
12+
from model_utils.managers import (
13+
InheritanceManager,
14+
JoinManager,
15+
QueryManager,
16+
SoftDeletableManager,
17+
SoftDeletableQuerySet,
18+
)
1319
from model_utils.models import (
1420
SoftDeletableModel,
1521
StatusModel,
@@ -19,7 +25,6 @@
1925
)
2026
from model_utils.tracker import FieldTracker, ModelTracker
2127
from tests.fields import MutableField
22-
from tests.managers import CustomSoftDeleteManager
2328

2429

2530
class InheritanceManagerTestRelated(models.Model):
@@ -347,10 +352,15 @@ class SoftDeletable(SoftDeletableModel):
347352
all_objects: ClassVar[Manager[SoftDeletable]] = models.Manager()
348353

349354

355+
class CustomSoftDeleteQuerySet(SoftDeletableQuerySet):
356+
def only_read(self):
357+
return self.filter(is_read=True)
358+
359+
350360
class CustomSoftDelete(SoftDeletableModel):
351361
is_read = models.BooleanField(default=False)
352362

353-
objects: ClassVar[CustomSoftDeleteManager[SoftDeletableModel]] = CustomSoftDeleteManager()
363+
available_objects = SoftDeletableManager.from_queryset(CustomSoftDeleteQuerySet)() # type: ignore[misc]
354364

355365

356366
class StringyDescriptor:

tests/test_managers/test_softdelete_manager.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,21 @@
66
class CustomSoftDeleteManagerTests(TestCase):
77

88
def test_custom_manager_empty(self):
9-
qs = CustomSoftDelete.objects.only_read()
9+
qs = CustomSoftDelete.available_objects.only_read()
1010
self.assertEqual(qs.count(), 0)
1111

1212
def test_custom_qs_empty(self):
13-
qs = CustomSoftDelete.objects.all().only_read()
13+
qs = CustomSoftDelete.available_objects.all().only_read()
1414
self.assertEqual(qs.count(), 0)
1515

1616
def test_is_read(self):
1717
for is_read in [True, False, True, False]:
18-
CustomSoftDelete.objects.create(is_read=is_read)
19-
qs = CustomSoftDelete.objects.only_read()
18+
CustomSoftDelete.available_objects.create(is_read=is_read)
19+
qs = CustomSoftDelete.available_objects.only_read()
2020
self.assertEqual(qs.count(), 2)
2121

2222
def test_is_read_removed(self):
2323
for is_read, is_removed in [(True, True), (True, False), (False, False), (False, True)]:
24-
CustomSoftDelete.objects.create(is_read=is_read, is_removed=is_removed)
25-
qs = CustomSoftDelete.objects.only_read()
24+
CustomSoftDelete.available_objects.create(is_read=is_read, is_removed=is_removed)
25+
qs = CustomSoftDelete.available_objects.only_read()
2626
self.assertEqual(qs.count(), 1)

0 commit comments

Comments
 (0)