Skip to content

Commit

Permalink
Auto-generate manager implementation for CustomSoftDelete
Browse files Browse the repository at this point in the history
Besides require less code, this also allows the django-stubs mypy
plugin to automatically generate an type-annotated version of the
manager.

Unfortunately, the plugin does not put a `ClassVar` annotation
on `objects`, which is why we need the suppression.
  • Loading branch information
mthuurne committed Apr 16, 2024
1 parent a86c14e commit 600d59c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
9 changes: 1 addition & 8 deletions tests/managers.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
from model_utils.managers import SoftDeletableManager, SoftDeletableQuerySet
from model_utils.managers import SoftDeletableQuerySet


class CustomSoftDeleteQuerySet(SoftDeletableQuerySet):
def only_read(self):
return self.filter(is_read=True)


class CustomSoftDeleteManager(SoftDeletableManager):
_queryset_class = CustomSoftDeleteQuerySet

def only_read(self):
return self.get_queryset().only_read()
11 changes: 8 additions & 3 deletions tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@

from model_utils import Choices
from model_utils.fields import MonitorField, SplitField, StatusField, UUIDField
from model_utils.managers import InheritanceManager, JoinManager, QueryManager
from model_utils.managers import (
InheritanceManager,
JoinManager,
QueryManager,
SoftDeletableManager,
)
from model_utils.models import (
SoftDeletableModel,
StatusModel,
Expand All @@ -19,7 +24,7 @@
)
from model_utils.tracker import FieldTracker, ModelTracker
from tests.fields import MutableField
from tests.managers import CustomSoftDeleteManager
from tests.managers import CustomSoftDeleteQuerySet


class InheritanceManagerTestRelated(models.Model):
Expand Down Expand Up @@ -350,7 +355,7 @@ class SoftDeletable(SoftDeletableModel):
class CustomSoftDelete(SoftDeletableModel):
is_read = models.BooleanField(default=False)

objects: ClassVar[CustomSoftDeleteManager[SoftDeletableModel]] = CustomSoftDeleteManager()
objects = SoftDeletableManager.from_queryset(CustomSoftDeleteQuerySet)() # type: ignore[misc]


class StringyDescriptor:
Expand Down

0 comments on commit 600d59c

Please sign in to comment.