Auto-generate manager implementation for CustomSoftDelete

Besides requiring less code, this also allows the django-stubs
mypy plugin to automatically generate a type-annotated version
of the manager.

Unfortunately, the plugin does not put a `ClassVar` annotation
on `objects`, which is why we need the suppression.
This commit is contained in:
Maarten ter Huurne 2024-04-16 02:51:07 +02:00
parent a86c14e4e7
commit 68a4c14c74
2 changed files with 9 additions and 11 deletions

View file

@ -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()

View file

@ -9,7 +9,12 @@ from django.utils.translation import gettext_lazy as _
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,
@ -19,7 +24,7 @@ from model_utils.models import (
)
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):
@ -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: